One-to-one and one-to-many

One-to-one and one-to-many relation in a database :


When creating your website, you may need to link different fields from different tables.

Our example to explain this concept will be very simple 😎 : 
You want to create a documentation with categories and articles where there are articles in each category.

Here are our two tables 'categories' and 'articles' :





Let's start by explaining the one-to-one relationship in order to link an article to a single category.


You have two choices, link them by value or by reference:

The main criteria to decide whether you use by reference or by value is if you want it to be static or updated at each modification :

- If you use by reference, you will need to do join operations to get the linked element but you will always have the updated value.
- If you use by value, you will directly have access to the value but if it changed in the other table, it won't change here. Another concern is that you will only have access to this field from the other table. When you use it by reference you can select other fields as well.

Go to your table and click to  :


Linked field (by value): You can change the data type.
categories: Select the table you want to link to.
name: Select the value of the column to display.
- Linked field by value:


In your edit row, you can now choose the value of the column that you have chosen beforehand :

πŸ“˜  NoteLinked field by value will store the value of the element in the selected table => "BlockChain"



- Linked field by reference:


In your edit row, you can now choose the value of the column that you have chosen beforehand :

πŸ“˜  NoteLinked field by reference will store the id of the element in the selected table => 1






That’s not all, it could be that an item could fall into more than one category, this is called a one-to-many relationship.

- Linked fields by value: 


In your edit row, you can now choose multiple values ​​of the column that you have chosen beforehand.



πŸ“˜  NoteLinked fields by value will store a JSON Array of values => ["Bitcoin","BlockChain"]


- Linked fields by reference: 



In your edit row, you can now choose multiple values ​​of the column that you have chosen beforehand.


πŸ“˜  NoteLinked fields by reference will store a JSON Array of ids => ["1","2"]