#23928: Raw queryset and use inner join
----------------------------------------------+----------------------------
     Reporter:  ivanff                        |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.7
     Severity:  Normal                        |   Keywords:  raw,
 Triage Stage:  Unreviewed                    |  select_related
Easy pickings:  0                             |  Has patch:  1
                                              |      UI/UX:  0
----------------------------------------------+----------------------------
 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))
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23928>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.baa9f175f6d2ea388cedbc6a491f41c3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to