Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - using multiple foreign key to the same model

Tags:

python

django

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).
like image 783
AlexW Avatar asked Jan 17 '17 12:01

AlexW


People also ask

Can a Django model have 2 foreign keys?

Models can have multiple foreign keys.

Can a model have multiple foreign keys?

A table can have multiple foreign keys based on the requirement.

Can you have multiple foreign keys from the same table Django?

Your intermediate model must contain one - and only one - foreign key to the source model (this would be Group in our example). If you have more than one foreign key, a validation error will be raised.

Does Django automatically index foreign keys?

Django automatically creates an index for all models. ForeignKey columns. From Django documentation: A database index is automatically created on the ForeignKey .


1 Answers

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:

  • Django: reverse accessors for foreign keys clashing

  • Django Reverse Accessor Clashes

like image 69
Moinuddin Quadri Avatar answered Oct 18 '22 03:10

Moinuddin Quadri