#21554: incorrect SQL generated when using multiple inheritance
----------------------------------------------+----------------------------
     Reporter:  pegler                        |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  master
     Severity:  Normal                        |   Keywords:  multiple-
 Triage Stage:  Unreviewed                    |  inheritance
Easy pickings:  0                             |  Has patch:  0
                                              |      UI/UX:  0
----------------------------------------------+----------------------------
 Our application makes use of multiple-inheritance and we've just come
 across an interesting bug.  There is a test case attached and it fails
 against the latest 1.4, 1.5, 1.6, and master versions.  Though the reason
 changes between versions 1.5 and 1.6.

 The models:
 {{{
 class Person(models.Model):
     name = models.CharField(max_length=50)

 class Politician(models.Model):
     politician_id = models.AutoField(primary_key=True)
     title = models.CharField(max_length=50)

 class Congressman(Person, Politician):
     state = models.CharField(max_length=2)

 class Senator(Congressman):
     pass
 }}}

 The statement {{{ Senator.objects.get(politician_id=1) }}} produces
 incorrect SQL for all versions, but is different between versions.

 1.4.10 and 1.5.5 result in the error "DoesNotExist: Senator matching query
 does not exist." due to it trying to join demo_politician onto
 demo_senator via demo_senator.congressman_ptr_id instead of
 demo_politician.politician_id

 {{{
 SELECT "demo_person"."id", "demo_person"."name", T5."politician_id",
 T5."title", "demo_congressman"."politician_ptr_id",
 "demo_congressman"."person_ptr_id", "demo_congressman"."state",
 "demo_senator"."congressman_ptr_id"
 FROM "demo_senator"
 INNER JOIN "demo_congressman" ON ("demo_senator"."congressman_ptr_id" =
 "demo_congressman"."person_ptr_id")
 INNER JOIN "demo_person" ON ("demo_senator"."congressman_ptr_id" =
 "demo_person"."id")
 INNER JOIN "demo_politician" T5 ON ("demo_senator"."congressman_ptr_id" =
 T5."politician_id")
 WHERE "demo_congressman"."politician_ptr_id" = 1
 }}}


 1.6 and master results in the error "OperationalError: no such column:
 demo_congressman.politician_id".  It incorrectly thinks that politician_id
 is on demo_congressman instead of demo_politician.

 {{{
 SELECT "demo_person"."id", "demo_person"."name",
 "demo_congressman"."politician_id", "demo_congressman"."title",
 "demo_congressman"."politician_ptr_id",
 "demo_congressman"."person_ptr_id", "demo_congressman"."state",
 "demo_senator"."congressman_ptr_id"
 FROM "demo_senator"
 INNER JOIN "demo_congressman" ON ( "demo_senator"."congressman_ptr_id" =
 "demo_congressman"."person_ptr_id" )
 INNER JOIN "demo_person" ON ( "demo_congressman"."person_ptr_id" =
 "demo_person"."id" )
 WHERE "demo_congressman"."politician_ptr_id" = 1
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21554>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.f784db0c2cec3678d7f4f18f1025b919%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to