Opened 8 years ago

Closed 13 months 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 timgraham)

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 8 years ago.
More complete example.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 8 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

duplicate of #3006

comment:2 Changed 8 years ago by miracle2k

  • Resolution duplicate deleted
  • Status changed from closed to reopened

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

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

Changed 8 years ago by ElliottM

More complete example.

comment:4 Changed 8 years ago by ElliottM

  • Triage Stage changed from Unreviewed to Accepted

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

  • Cc ozgurisil@… added

comment:6 Changed 5 years ago by SmileyChris

  • Severity set to Normal
  • Type set to New feature

comment:7 Changed 5 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:8 Changed 5 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:9 Changed 3 years ago by aaugustin

  • Status changed from reopened to new

comment:10 Changed 13 months ago by timgraham

  • Description modified (diff)
  • Resolution set to fixed
  • Status changed from new to closed

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