16

I'm trying to use the same foreign key for two fields in the same model and am getting an error.

Im trying to have a primary and secondary on call user, but am not sure how to format the relationship after receiving the errors below

class ManualRotas(models.Model):
    rota_name = models.CharField(max_length=200,choices=settings.ONCALL_ROTAS)
    primary_user = models.ForeignKey(User, unique=True, verbose_name="Primary OnCall Engineer")
    p_start_time = models.DateTimeField(verbose_name="Start Time")
    p_end_time = models.DateTimeField(verbose_name="End Time")
    secondary_user = models.ForeignKey(User, verbose_name="Backup OnCall Engineer", unique=True,blank=True,null=True)
    s_start_time = models.DateTimeField(blank=True,null=True, verbose_name="Start Time")
    s_end_time = models.DateTimeField(blank=True,null=True,verbose_name="Start Time")


ERRORS:
oncall.ManualRotas.primary_user: (fields.E304) Reverse accessor for 'ManualRotas.primary_user' clashes with reverse accessor for 'ManualRotas.secondary_user'.
        HINT: Add or change a related_name argument to the definition for 'ManualRotas.primary_user' or 'ManualRotas.secondary_user'.
oncall.ManualRotas.secondary_user: (fields.E304) Reverse accessor for 'ManualRotas.secondary_user' clashes with reverse accessor for 'ManualRotas.primary_user'.
        HINT: Add or change a related_name argument to the definition for 'ManualRotas.secondary_user' or 'ManualRotas.primary_user'.

WARNINGS:
oncall.ManualRotas.primary_user: (fields.W342) Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.
        HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
oncall.ManualRotas.secondary_user: (fields.W342) Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.
        HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.

System check identified 4 issues (0 silenced).
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
AlexW
  • 2,843
  • 12
  • 74
  • 156
  • 5
    HINT: Add or change a related_name argument to the definition for 'ManualRotas.secondary_user' or 'ManualRotas.primary_user'. – e4c5 Jan 17 '17 at 12:55
  • Have you read the errors you've received? Have you tried researching what it says? Have you tried following the hint? – Sayse Jan 17 '17 at 13:04
  • Possible duplicate of [How can I have two foreign keys to the same model in Django?](https://stackoverflow.com/questions/543377/how-can-i-have-two-foreign-keys-to-the-same-model-in-django) – Aaron McMillin Aug 14 '17 at 14:33

1 Answers1

28

You have to define different related_name to both the ForeignKeys columns. For example:

class ManualRotas(models.Model):
    primary_user = models.ForeignKey(User, related_name='related_primary_manual_roats', unique=True, verbose_name="Primary OnCall Engineer")
    #                            related names ^ v
    secondary_user = models.ForeignKey(User, related_name='related_secondary_manual_roats', verbose_name="Backup OnCall Engineer", unique=True,blank=True,null=True)
    # .... Other columns

Please also refer ForeignKey.related_name document, which says:

The name to use for the relation from the related object back to this one. It’s also the default value for related_query_name (the name to use for the reverse filter name from the target model). See the related objects documentation for a full explanation and example. Note that you must set this value when defining relations on abstract models; and when you do so some special syntax is available.


Related posts:

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126