[Django] #24090: ORM (still!) neglects to use aliases it has set up when certain multiple subqueries are used AND multi table inheritance is in use

20 views
Skip to first unread message

Django

unread,
Jan 7, 2015, 5:56:11 AM1/7/15
to django-...@googlegroups.com
#24090: ORM (still!) neglects to use aliases it has set up when certain multiple
subqueries are used AND multi table inheritance is in use
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) | Keywords: orm subquery alias
Severity: Normal | multi table inheritance mth
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
This is following on from ticket #23605, which was successfully fixed but
didn't fix our specific test case.

This turns out to be because our test case also used multi table
inheritance.

So, details are basically the same as #23605, but using django 1.7.2 and
the test models.py is:

{{{
from django.db import models

class ModelAParent ( models.Model ):
pass

class ModelA ( ModelAParent ):
pass

class ModelB ( models.Model ):
modela_fk = models.ForeignKey ( ModelA )
modelc_fk = models.ForeignKey ( "ModelC" )

field_b0 = models.IntegerField ( null = True )
field_b1 = models.BooleanField ()

class ModelC ( models.Model ):
field_c0 = models.FloatField ()
}}}

The query is the same:

{{{
ModelA.objects.filter (
Q ( pk__in = ModelA.objects.filter ( Q ( modelb__field_b0__gte =
1000000 / F ( "modelb__modelc_fk__field_c0" ) )
& Q ( modelb__field_b1__exact = True )
& ~Q ( modelb__pk__in = ModelB.objects.filter (
~(
Q ( field_b1__exact = True )
& Q ( field_b0__gte = 1000000 / F ( "modelc_fk__field_c0"
) )
)
) )
).filter ( modelb__field_b1__exact = True )
) )
}}}

Used against postgres 9.1 generates the error:

{{{
ProgrammingError: invalid reference to FROM-clause entry for table
"dummy_modelb"
LINE 1: ...") AND U0."field_b0" IS NOT NULL)) AND V1."id" = (dummy_mode...
^
HINT: Perhaps you meant to reference the table alias "v1".
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24090>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 7, 2015, 9:50:07 AM1/7/15
to django-...@googlegroups.com
#24090: ORM (still!) neglects to use aliases it has set up when certain multiple
subqueries are used AND multi table inheritance is in use
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm subquery alias | Triage Stage: Accepted
multi table inheritance mth |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


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

Django

unread,
Jul 13, 2015, 4:05:52 AM7/13/15
to django-...@googlegroups.com
#24090: ORM (still!) neglects to use aliases it has set up when certain multiple
subqueries are used AND multi table inheritance is in use
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm subquery alias | Triage Stage: Accepted
multi table inheritance mth |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by akaariai):

This ticket was fixed by b68212f539f206679580afbfd008e7d329c9cd31. I'm
adding a pull request for tests.

--
Ticket URL: <https://code.djangoproject.com/ticket/24090#comment:2>

Django

unread,
Jul 13, 2015, 1:14:21 PM7/13/15
to django-...@googlegroups.com
#24090: ORM (still!) neglects to use aliases it has set up when certain multiple
subqueries are used AND multi table inheritance is in use
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm subquery alias | Triage Stage: Accepted
multi table inheritance mth |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"76ac07a90967acb0225d0a590164ae1b7ad0e6bd" 76ac07a9]:
{{{
#!CommitTicketReference repository=""
revision="76ac07a90967acb0225d0a590164ae1b7ad0e6bd"
Refs #24090 -- Added a test for multi-table inheritance + subqueries.

Ticket #24090 was already fixed by
b68212f539f206679580afbfd008e7d329c9cd31, this commit adds tests to
verify this is indeed the case.

Thanks to Beauhurst for commissioning the work on this ticket.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24090#comment:3>

Django

unread,
Jul 13, 2015, 1:18:00 PM7/13/15
to django-...@googlegroups.com
#24090: ORM (still!) neglects to use aliases it has set up when certain multiple
subqueries are used AND multi table inheritance is in use
-------------------------------------+-------------------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: orm subquery alias | Triage Stage: Accepted
multi table inheritance mth |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/24090#comment:4>

Reply all
Reply to author
Forward
0 new messages