﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
3906	Generic Relation query generates incorrect SQL	Jamie.Norrish@…	nobody	"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."		closed	Contrib apps	0.96		fixed	generic	mir@…	Unreviewed	1	0	0	0	0	0
