﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23928	Raw queryset and use inner join	Ivan	nobody	"when I used raw queryset I was faced with incorrect mapping of fields in the output if the sql query was attended by JOIN. [id, name, test id, key] returns a field from a mysql cursor, due to duplication of columns was offset to get the value of id!

I temporarily have corrected this by extracting the first occurrence of the name column:

Note the line ""if column not in model_init_field_names:""

{{{
class MyRawQuerySet(RawQuerySet):

    def __iter__(self):
        # Mapping of attrnames to row column positions. Used for constructing
        # the model using kwargs, needed when not all model's fields are present
        # in the query.
        model_init_field_names = {}
        # A list of tuples of (column name, column position). Used for
        # annotation fields.
        annotation_fields = []

        # Cache some things for performance reasons outside the loop.
        db = self.db
        compiler = connections[db].ops.compiler('SQLCompiler')(
            self.query, connections[db], db
        )
        need_resolv_columns = hasattr(compiler, 'resolve_columns')

        query = iter(self.query)

        try:
            # Find out which columns are model's fields, and which ones should be
            # annotated to the model.
            for pos, column in enumerate(self.columns):
                if column not in model_init_field_names:
                    if column in self.model_fields:
                        model_init_field_names[self.model_fields[column].attname] = pos
                    else:
                        annotation_fields.append((column, pos))
}}}"	Bug	closed	Database layer (models, ORM)	1.7	Normal	needsinfo	raw, select_related		Unreviewed	1	0	0	0	0	0
