Have trouble understanding the part about a Foreign Key at tutorial part 2.

So in class Choice(models.Model), a variable “question” is declared with its allocation being “models.ForeignKey(Question, on_delete=models.CASCADE)”. But I don’t understand what this allocation actually does, as in, what does the second argument, “on_delete=models.CASCADE” even mean?

TL;DR: It means that when the Question is removed, its choices are automatically removed as well.


More context
I haven’t followed the tutorial so I can’t speak for that part. But to understand a Foreign Key, a little intro in databases:

Databases work with tables of information and we can relate the information within these tables to each other.
But there are trade-offs in relational databases. Examples are:

  • A table is fixed, i.e. no columns can (or rather should) be added after it’s creation.
  • For performance and storage, we only want necessary data stored. Thus empty cells or NULL values are preferably few as possible

Without delving to deep, this means we normalise (see Google) a database. Basically, we break our information in more smaller tables.

Example
An enterprise can have one or more establishments. Rather than placing this information in one table

id  | name         | name establ.    | name establ.    | name establ.    
------------------------------------------------------------------------
001 | enterprise 1 | establishment 1 | NULL            | NULL
002 | enterprise 2 | establishment 1 | establishment 2 | establishment 3

We break it up in two tables, like so:

enterprises        |
id  | name         | 
-------------------
001 | enterprise 1 |
002 | enterprise 2 | 
establishments        |
id  | name establ.    | 
----------------------
001 | establishment 1 | 
002 | establishment 1 | 
003 | establishment 2 |
004 | establishment 3 |

But this leaves the problem that we don’t know to which enterprise establishment 1 belongs.

Enter the Foreign Key:

establishments                                   |  
id  | name establ.    | foreign key (enterprises)|
-------------------------------------------------|
001 | establishment 1 | 001                      |
002 | establishment 1 | 002                      |
003 | establishment 2 | 002                      |
004 | establishment 3 | 002                      |

Now we see that the first establishment 1 points to the record 001 in table enterprises , and so on.

This leaves us with one more question, “what do we want to do, when enterprise 1 is deleted from our database?”

Typically this means you’d want to delete all related establishments as well.
Hence on_delete=models.CASCADE, that is “cascade like a waterfall”.
If the parent (enterprise) is deleted, we want to automatically delete the child (establishment).

Other options and their actions can be found here.

Hi,
This essentially means that the Choice model has a Foreign Key pointing to the Question model, which defines a one-to-many relationship where one Question can have multiple Choice instances associated with it.

The on_delete=models.CASCADE option means that if a Question is deleted, whether by an admin or the user, all related Choice objects will also be deleted automatically from the database.