Opened 16 years ago

Closed 9 years ago

#9899 closed New feature (fixed)

GenericRelation of a childmodel can't be used in lookup — at Version 10

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):
    pass

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 )

Change History (11)

comment:1 by anonymous, 16 years ago

Resolution: duplicate
Status: newclosed

duplicate of #3006

comment:2 by miracle2k, 16 years ago

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 by anonymous, 16 years ago

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

by ElliottM, 16 years ago

Attachment: test.zip added

More complete example.

comment:4 by ElliottM, 16 years ago

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 'example.py'

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 by anonymous, 15 years ago

Cc: ozgurisil@… added

comment:6 by Chris Beaven, 14 years ago

Severity: Normal
Type: New feature

comment:7 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:10 by Tim Graham, 9 years ago

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