#31657: Self referencing foreign key doesn't correctly order by a relation "_id"
field.
-------------------------------------+-------------------------------------
Reporter: Jack Delany | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jack Delany):
I did some additional work on this to verify the scope of the change. I
added the following **test** code on master and ran the entire test suite:
{{{
+++ b/django/db/models/sql/compiler.py
@@ -727,6 +727,11 @@ class SQLCompiler:
# If we get to this point and the field is a relation to another
model,
# append the default ordering for that model unless it is the pk
# shortcut or the attribute name of the field that is specified.
+ if (field.is_relation and opts.ordering and name != 'pk' and
+ ((getattr(field, 'attname', None) != name) !=
+ (getattr(field, 'attname', None) != pieces[-1]))):
+ print(f"JJD <{getattr(field, 'attname', '')}> <{name}>
<{pieces[-1]}>")
+ breakpoint()
if field.is_relation and opts.ordering and getattr(field,
'attname', None) != name and name != 'pk':
# Firstly, avoid infinite loops.
already_seen = already_seen or set()
}}}
The idea being to display every time that the change from name to
pieces[-1] in the code would make a difference in the execution. Of
course verified that when running the reproducer one does go into the test
block and outputs: {{{JJD <root_id> <record__root_id> <root_id>}}}. The
code is not triggered for any other test across the entire test suite, so
the scope of the change is not causing unexpected changes in other places.
This seems reassuring.
--
Ticket URL: <https://code.djangoproject.com/ticket/31657#comment:7>
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/066.c9410ac62d3369a9da4d42a82a9fc194%40djangoproject.com.