#3906: Generic Relation query generates incorrect SQL
------------------------------------+---------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: jacob
Status: new | Component: Uncategorized
Version: 0.96 | Keywords:
Stage: Unreviewed | Has_patch: 0
------------------------------------+---------------------------------------
Given the following models:
{{{
#!python
class Entity (models.Model):
entity_type = models.CharField(maxlength=30)
class PropertyAssertion (models.Model):
entity = models.ForeignKey(Entity)
assertion = models.GenericForeignKey()
content_type = models.ForeignKey(ContentType, core=True)
object_id = models.PositiveIntegerField()
class Name (models.Model):
display_form = models.CharField(maxlength=400, blank=True)
assertion = models.GenericRelation(PropertyAssertion)
}}}
having a method on the Entity class which performs
{{{
#!python
Name.objects.filter(assertion__entity=self,
assertion__content_type=name_content_type)
}}}
(where name_content_type is whatever the content_type of the Name model is
in the particular database), the generated SQL (slightly abbreviated in
terms of the fields selected) is bogus:
{{{
#!sql
SELECT "eats_core_name".*
FROM "eats_core_name" LEFT OUTER JOIN "eats_core_propertyassertion" AS
"m2m_eats_core_name__assertion"
ON "eats_core_name"."id" = "m2m_eats_core_name__assertion"."object_id"
INNER JOIN "eats_core_propertyassertion" AS "eats_core_name__assertion"
ON "m2m_eats_core_name__assertion"."object_id" =
"eats_core_name__assertion"."id"
WHERE ("m2m_eats_core_name__assertion"."entity_id" = 1 AND
"m2m_eats_core_name__assertion"."content_type_id" = 31)
}}}
This is joining the propertyassertion table to itself, linked by an
identity between object_id and id, which have nothing to do with each
other. The results of this query are predictably incorrect.
I don't know what the query was which 0.95 generated, but the code seemed
to work then and does not with 0.96.
--
Ticket URL: <http://code.djangoproject.com/ticket/3906>
Django Code <http://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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---