Generic Relation query generates incorrect SQL
|Reported by:||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Given the following models:
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
(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:
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.
Change History (8)
comment:2 Changed 9 years ago by
|Component:||Uncategorized → Contrib apps|
|Owner:||changed from Jacob to Adrian Holovaty|