Relation types
Last updated
Last updated
In an M2O relation, multiple items from the parent table are linked to one item in a related table. For example, there are many cities in a country, but a city can only be in one country.
To create an M2O relationship, we add a foreign key field to the parent table, which links items from the parent table to items in the related table. If we have two tables, cities
and countries
, we can create a cities.country_id
foreign key field.
Let's take a look at the schema.
cities- id- name- country_id (a foreign key field, stores a key from countries.id)
countries- id- name
Note the following things from the schema above:
An M2O relationship requires just one column within the parent table.
When an M2O relational field is configured in U-code, an Item Page on the parent table will enable access to the item from the related table. So in our example above, an Item Page in cities
will enable access to the related country from the countries
table.
However, in the U-code, an M2O field does not automatically provide access to the parent table's items within the related table. In our example, this means that when user open an Item Page in countries
, user will not see related cities
Configure an M2O
The easiest way to configure an M2O field is to follow the guide on how to create a field (standard) and select the M2O Interface from the template wizard.
The relation types we have seen so far only required one foreign key column to link the parent table and related table. An M2M relation is composed of two foreign key columns stored within an additional table, called a junction table, which stores each linked row between the parent table and related table.
Junction tables are required in M2M relations because the number of relation created can (and often will!) outnumber the number of rows in either data table. In other words, if user have x
rows in the parent column and y
rows in the related column, user need room to store up to x * y
rows. Junction tables provide a place to store all the relations between rows, no matter how many exist.
To demonstrate this, let's think about the relation between recipes and ingredients: a recipe can have many ingredients, and ingredients can be in many recipes.
Let's take a look at the schema.
recipes- id- name- ingredients (An M2M alias field. Does not exist in the database, allows access to ingredients linked from recipe_ingredients)
recipes_ingredients (junction
table)- id- recipe (stores foreign key from recipes.id)- ingredient (stores foreign key from ingredients.id)- quantity (A "context" field. Stores other data associated with the relation. These are optional.)
ingredients- id- name
Note the following points from the schema above. When we create an M2M in U-Code:
Our junction table, recipe_ingredients
, each row contains two foreign key columns. This is what creates the relations between the two tables.
Assuming the M2M alias field is created within the recipes
table, U-Code does not automatically add a field to display recipes within the ingredients
table. However,user can easily add such a field when creating the M2M field within the recipes
table, by clicking on Continue in Advanced Field Creation Mode
, opening the Relation
tab and selecting Add M2M to "ingredients"
:
ingredients- id- name- recipes (an M2M alias field, does not exist in the database, enables access to all the recipes related to an ingredient)
Notice that the junction table also has a quantity
field, which tracks how much of each ingredient is needed for the recipe. This is called a contextual field.
User can also have a self-referencing M2M relation that connects items in the same table. One example is "Related Articles", where each article relates to many other articles.
Configure an M2M
The easiest way to configure an M2M is to follow the guide on how to create a field (standard) and select Many to Many from the template wizard.