Code

#20874 closed Bug (fixed)

Nested subqueries have conflicting alias prefixes

Reported by: akaariai Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently the ORM has a way to bump table alias prefixes for subqueries. This means that for example alias T0 would be turned to U0. Unfortunately the current way doesn't work when nesting multiple queries, that is in cases like:

qs1 = MyModel.objects.all()
qs2 = MyModel.objects.filter(pk__in=qs1)
qs3 = MyModel.objects.filter(pk__in=qs2)

for qs2 the qs1 gets bumped to U-prefixes. But qs2 gets bumped *again* to U-prefixes in qs3. Luckily this doesn't usually matter that much in current Django, as for example "u0.id IN (SELECT u0.id FROM sometable u0) does work. Later on, if EXISTS queries are going to be more common this will become more important as "WHERE EXISTS (SELECT ... WHERE u0.id = u0.id) doesn't work at all.

Attachments (0)

Change History (2)

comment:1 Changed 11 months ago by akaariai

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Ready for checkin

Patch available at https://github.com/akaariai/django/tree/ticket_20874

I think this one is ready for checkin, but as the fix is nontrivial I will leave this open for reviews.

comment:2 Changed 11 months ago by Anssi Kääriäinen <akaariai@…>

  • Resolution set to fixed
  • Status changed from new to closed

In dcdc579d162b750ee3449e34efd772703592faca:

Fixed #20874 -- bump_prefix() in nested subqueries

Also made some cleanup to build_filter() code by introducing submethods
solve_lookup_type() and prepare_lookup_value().

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.