Database relationships in Django

I have procrastinated on this today, it's almost 10am and I was supposed to write this almost an hour ago. Next on the list of Django components to understand is database relationships. Really this isn't specific to Django at all, it's about data modelling and database design. There are 3 types of relationship in a relational database: a Foreign Key, One to One and Many to Many. Django provides fields for all these out of the box.

I find the easiest way to think about how they work with plain English (or language of your choosing!). Today let's start with the simplest One to One. The most common use case for this is a User Profile table being linked to a User table. The English I think about goes like this:

"A User can only have a single Profile, and a Profile can only belong to a single User".

Reading that should reveal what a One to One is all about, allowing a single row to be linked to only another single row and not multiple rows. It is the most restrictive out of the three relationship types and generally the least used in my experience.

Tomorrow we will talk about the most common, Foreign Keys.