#33682: SQL generation bug in `.distinct()` when supplied fields go through
multiple many-related tables
-------------------------------------+-------------------------------------
Reporter: Robert Leach | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sql, distinct, | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Robert Leach):
I figured I might have been missing something. I actually have code to
avoid the gotcha, but apparently, I only did it for the model from which
the query came from (i.e. the "root model"):
{{{
# If there are any split_rows manytomany related tables, we will
need to prepend the ordering (and pk) fields of
# the root model
if len(distinct_fields) > 0:
distinct_fields.insert(0, "pk")
tmp_distincts =
self.getOrderByFields(model_name=self.rootmodel.__name__)
tmp_distincts.reverse()
for fld_nm in tmp_distincts:
distinct_fields.insert(0, fld_nm)
if order_by is not None and order_by not in distinct_fields:
distinct_fields.insert(0, order_by)
}}}
{{{
def getOrderByFields(self, mdl_inst_nm=None, model_name=None):
"""
Retrieves a model's default order by fields, given a model
instance name.
"""
... brevity edit ...
# Get a model object
mdl = apps.get_model("DataRepo", mdl_nm)
if "ordering" in mdl._meta.__dict__:
return mdl._meta.__dict__["ordering"]
return []
}}}
I know this ticket system is not the place for getting support, but if
you'll indulge me... would prepending all the meta ordering fields avoid
the gotcha if I inserted the meta ordering field(s) before any other
fields? (In my use case, the order is unimportant - only the distinct
is). I'd found that it did in the case of the "root model".
--
Ticket URL: <https://code.djangoproject.com/ticket/33682#comment:2>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018099f50604-119e789f-ef03-417f-99bc-f67f3ddb2895-000000%40eu-central-1.amazonses.com.