I was watching a video on django-orm and the instructor stated that:
We should set
primary_key=Trueto prevent a Model from having duplicate rows in a OneToOne relationship (Ex: Prevent a user from having multiple profiles).
I know this statement is wrong! AFAIK, an OneToOne field is just a ForeignKey with unique parameter set to True. But I got curious and looked-up the Django documentation, Sure enough they are using primary=True in their example.
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
class Restaurant(models.Model):
place = models.OneToOneField(
Place,
on_delete=models.CASCADE,
primary_key=True,
)
So, why would someone set primary_key=True on an OneToOne relation?
My guess is that it's just reasonable to have that field as a primary key and there is no technical background behind it.