Opened 9 years ago

Closed 8 years ago

#2573 closed defect (invalid)

Relating two models to the same model using generic relations with the same parameters causes conflicts

Reported by: clong Owned by: adrian
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords: generic relations, database
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Using the following models:

class Group(models.Model):
...
    row_level_permissions_owned = models.GenericRelation(RowLevelPermission, object_id_field="owner_id", content_type_field="owner_ct", related_name="owner")
...

class User(models.Model):
...
    row_level_permissions_owned = models.GenericRelation(RowLevelPermission, object_id_field="owner_id", content_type_field="owner_ct", related_name="owner")
...

class RowLevelPermission(models.Model):
class RowLevelPermission(models.Model):
...
    owner_id = models.PositiveIntegerField("'Owner' ID")
    owner_ct = models.ForeignKey(ContentType, verbose_name="'Owner' content type", related_name="owner_ct")
...    
    model = models.GenericForeignKey(fk_field='model_id', ct_field='model_ct')
    owner = models.GenericForeignKey(fk_field='owner_id', ct_field='owner_ct')

Causes the following errors:

auth.user: Accessor for m2m field 'row_level_permissions_owned' clashes with related m2m field 'RowLevelPermission.owner'. Add a related_name argument to the definition for 'row_level_permissions_owned'.
auth.user: Reverse query name for m2m field 'row_level_permissions_owned' clashes with related m2m field 'RowLevelPermission.owner'. Add a related_name argument to the definition for 'row_level_permissions_owned'.
auth.group: Accessor for m2m field 'row_level_permissions_owned' clashes with related m2m field 'RowLevelPermission.owner'. Add a related_name argument to the definition for 'row_level_permissions_owned'.
auth.group: Reverse query name for m2m field 'row_level_permissions_owned' clashes with related m2m field 'RowLevelPermission.owner'. Add a related_name argument to the definition for 'row_level_permissions_owned'.

By changing the two related_names to be different from another will stop this error. As long as the two related_names are the same (no matter what they are) the error happens.

From what I've been able to determine this happens because Django believes this to be a M2M relationship that contributes an attribute to the related class (which it does not) and having two attributes of the same name for two different relationships on the same object causes it to believe there is an error.

Change History (1)

comment:1 Changed 8 years ago by SmileyChris

  • Resolution set to invalid
  • Status changed from new to closed

Doesn't look like a bug to me unless I'm missing something.

You can't have two generic or foreign keys with identical related_names pointing to the same model.

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