Opened 9 years ago

Closed 3 years ago

#9899 closed New feature (fixed)

GenericRelation of a childmodel can't be used in lookup

Reported by: miracle2k Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Normal Keywords:
Cc: ozgurisil@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

class Feature(models.Model):
    object_id = models.PositiveIntegerField()
    content_type = models.ForeignKey(ContentType)
    content_object = GenericForeignKey()
    is_public = models.BooleanField()

class Base(models.Model):

class Child(models.Model):
    features = GenericRelation(Feature)
>>> Child.objects.filter(features__is_public=False).count()
<class '_mysql_exceptions.OperationalError'>: (1054, "Unknown column 'app_feature.base_ptr_id' in 'where clause'")

The query that is executed:

SELECT COUNT(*) FROM `app_child` INNER JOIN `app_feature` ON (`app_child`.`feed_ptr_id` = `app_feature`.`object_id`) INNER JOIN `app_feature` T4 ON (`app_feature`.`feed_ptr_id` = T4.`id`) WHERE (`app_feature`.`feed_ptr_id` IS NOT NULL AND T4.`content_type_id` = %s )

Attachments (1) (3.2 KB) - added by ElliottM 9 years ago.
More complete example.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 9 years ago by anonymous

Resolution: duplicate
Status: newclosed

duplicate of #3006

comment:2 Changed 9 years ago by miracle2k

Resolution: duplicate
Status: closedreopened

I'm reoponing for now, please confirm this is really a duplicate, because #3006 refers to GenericForeignKey, whereas this is about means GenericRelation. Using the same setup/query in a non-inheritance scenario *does* in fact work.

comment:3 Changed 9 years ago by anonymous

oh, sorry, i saw "generic" and just assumed it was the same.

Changed 9 years ago by ElliottM

Attachment: added

More complete example.

comment:4 Changed 9 years ago by ElliottM

Triage Stage: UnreviewedAccepted

I added a project that shows exactly how to do it. Unzip it, change the DB info, set DJANGO_SETTINGS_MODULE to 'test.settings', run syncdb, then run ''

An interesting note is that if you set the "db_column" attribute of the cabinet's o2o field to "id", the query gives the expected results, at least in the most basic of circumstances.

comment:5 Changed 9 years ago by anonymous

Cc: ozgurisil@… added

comment:6 Changed 7 years ago by Chris Beaven

Severity: Normal
Type: New feature

comment:7 Changed 6 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 Changed 6 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 Changed 5 years ago by Aymeric Augustin

Status: reopenednew

comment:10 Changed 3 years ago by Tim Graham

Description: modified (diff)
Resolution: fixed
Status: newclosed

I couldn't reproduce as far back as 1.4, so I guess this has been fixed between 1.0 and then.

Note: See TracTickets for help on using tickets.
Back to Top