[Django] #23928: Raw queryset and use inner join

200 views
Skip to first unread message

Django

unread,
Nov 28, 2014, 6:17:39 AM11/28/14
to django-...@googlegroups.com
#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.

Django

unread,
Nov 28, 2014, 6:59:09 AM11/28/14
to django-...@googlegroups.com
#23928: Raw queryset and use inner join
-------------------------------------+-------------------------------------
Reporter: ivanff | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.7
(models, ORM) | Resolution: needsinfo
Severity: Normal | Triage Stage:
Keywords: raw, select_related | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by akaariai):

* status: new => closed
* needs_docs: => 0
* resolution: => needsinfo
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

It isn't clear how to reproduce the error you are seeing from the
description. Could you post example models and the raw query that is
causing the problem for you. I'll close this needsinfo, please reopen when
you provide the additional information.

--
Ticket URL: <https://code.djangoproject.com/ticket/23928#comment:1>

Reply all
Reply to author
Forward
0 new messages