1

Can someone explain the significance of specifying a relationship in a Django model as one-to-one as opposed to just a foreign key?

Specifically, I'm wondering what advantages you get from specifying a relationship as 1-1, if any.

Thanks so much.

eikonomega
  • 1,971
  • 17
  • 28
  • I don't use Django - and as such I find the question extra confusing - but it sounds like researching the debate about "NULL FOREIGN KEYS" (search terms) *might* be a good starting point .. –  Oct 29 '12 at 02:37
  • possible duplicate of [What's the difference between django OneToOneField and ForeignKey?](http://stackoverflow.com/questions/5870537/whats-the-difference-between-django-onetoonefield-and-foreignkey) – danodonovan May 12 '14 at 08:10

4 Answers4

10

The OneToOneField evolved in Django after 'ForeignKey'. Conceptually a ForeignKey with unique=True constraint is similar to OneToOneField.

So if you want to ensure that every picture has one user and vice-versa use a OneToOneField.

If you want one user to have any number of pictures use a ForeignKey.

The way things are selected are also different. In case of doing OneToOneField, you can do user.picture and get the picture directly. In case of ForeignKey you will do user.picture_set[0] to get the first picture or access all the pictures associated with that user.

MultiTableInheritance implicitly uses OneToOneField internally and you can see where the concept originated from.

Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
2

They are not the same; think about it:

If you have a one-to-one relationship between a User and a Picture, you are saying that a user can only have one picture (and a picture can only have one user). If you were to have a Picture with foreign key to User, then you are saying that a picture must have exactly one user, but a user may have 0, 1 or many pictures.

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
  • This doesn't explain anything about what is actually going on. A 1-to-1 is still an fkey under the hood (just with tighter constraints in one place or another). – Matt Whipple Oct 29 '12 at 03:35
  • Thanks @sampson-chen. I understand the difference what a 1-1 relationship is as a opposed to a generic key, but I interested in how that plays out in Django. – eikonomega Oct 29 '12 at 19:13
2

The additional constraints of a 1-1 provide a tighter and richer conceptual model but can also provide insight that can allow for more intuitive retrieval. As a many to one represents a parent/collection relationship there is an unclear cost associated with the retrieval of any given collection for a particular entity. Since a 1-1 provides a flat mapping there is also a flat cost of retrieval. This would lead to things like preferring eager fetching when relevant, as the join will be be able to be easily optimized and the resultant data set will be a known size.

Matt Whipple
  • 7,034
  • 1
  • 23
  • 34
0

Django's Foreign Key is a many to one relationship. Now, the difference between them is the same as the difference between a One-To-One and Many-To-One relationship. For example, if you have User and Profile entities. You would like to add a constraint that each User could have one and only one Profile. Then, using a django's one to one field would create a restriction on the database level so, that you won't be able to relate User to multiple Profiles or vice-versa. Where as using Foreign Key wouldn't provide this constraint.

Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62