Opened 9 years ago

Closed 8 years ago

#24090 closed Bug (fixed)

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

Reported by: ris Owned by: nobody
Component: Database layer (models, ORM) Version: 1.7
Severity: Normal Keywords: orm subquery alias multi table inheritance mth
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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".

Change History (4)

comment:1 Changed 9 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 8 years ago by Anssi Kääriäinen

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

comment:3 Changed 8 years ago by Tim Graham <timograham@…>

In 76ac07a9:

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.

comment:4 Changed 8 years ago by Tim Graham

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top