Opened 17 years ago
Closed 17 years ago
#7056 closed (fixed)
Query.as_sql() has side effects for select_related queries
Reported by: | Erin Kelly | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | queryset-refactor |
Severity: | Keywords: | qs-rf | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Each time Query.as_sql() is called on a select_related query, the list of select columns grows again. Presumably Query.pre_sql_setup() should only get called once? For example:
In [39]: qs = Species.objects.all().select_related(depth=1) In [40]: qs.query.as_sql() Out[40]: ('SELECT "TEST_SPECIES"."ID", "TEST_SPECIES"."NAME", "TEST_SPECIES"."GENUS_ID", "TEST_GENUS"."ID", "TEST_GENUS"."NAME", "TEST_GENUS"."FAMILY_ID" FROM "TEST_SPECIES" INNER JOIN "TEST_GENUS" ON ("TEST_SPECIES"."GENUS_ID" = "TEST_GENUS"."ID")', ()) In [41]: qs.query.as_sql() Out[41]: ('SELECT "TEST_SPECIES"."ID", "TEST_SPECIES"."NAME", "TEST_SPECIES"."GENUS_ID", "TEST_GENUS"."ID", "TEST_GENUS"."NAME", "TEST_GENUS"."FAMILY_ID", "TEST_SPECIES"."ID", "TEST_SPECIES"."NAME", "TEST_SPECIES"."GENUS_ID", "TEST_GENUS"."ID", "TEST_GENUS"."NAME", "TEST_GENUS"."FAMILY_ID" FROM "TEST_SPECIES" INNER JOIN "TEST_GENUS" ON ("TEST_SPECIES"."GENUS_ID" = "TEST_GENUS"."ID")', ())
Change History (3)
comment:1 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 17 years ago
comment:3 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
We can't really arrange to call
pre_sql_setup()
just once, because if we did that and then added something to the query set, it could change the required select columns and we wouldn't know how to undo the previous select modifications frompre_sql_setup()
. So the sequenceand similar usage patterns wouldn't be correct.
I've fixed this by keeping the extra select columns separate until we construct the query in
as_sql()
.