Relations represent the connection between models. The relation type determines the connection between individual models. Models can have one or more different types of connections. Let's break those down in this article.
Note: Before continuing with this article, make sure you understand What is a data model?
Creating a relation
Before defining relation types, have a look at a simple example with two data models and create a relation between them.
Imagine you have a project management application that contains two individual data models:
Project model with properties like Name, Status, Description, etc.
Task model with similar properties: Name, Status, Time spent, etc.
Naturally, you want any project to contain smaller pieces of work to complete it step-by-step. This is where relations come into play - we have to connect the Project and Task models so that 'One Project has many Tasks'. The easiest way to do so is via the canvas view in Models:
In the case of 'One Project has many Tasks', we can call Project a source model - the one we connect from, and Task - a target model, the one we connect the source model to.
Relation types
Data models on Betty Blocks enable establishing three types of relations: Belongs to (many to one), Has many (one to many), and Has and belongs to many (one has and belongs to many).
Belongs to (many sources belong to one target)
| Multiple objects as defined in the source model are related to a single object as defined by the target model.
The source model (Task) obtains a new record variable that will store an object of the target model (Task.Project)
The target model (Project) obtains a new collection variable that stores multiple objects of the source model (Project.Tasks). | Examples:
Many Tasks are assigned to one Team.
Many Players belong to one Team.
Many Reservations belong to one Room.
|
Has many (one source has many targets)
| This means that one object of the source model is related to multiple objects of the target model.
The source model (Project) obtains a new collection variable that stores multiple objects of the target model (Project.Tasks).
The target model (Task) obtains a new record variable that stores an object of the source model (Task.Project). | Examples:
Movie has many Actors playing in it.
Pizza is made out of many Ingredients.
One Room has many Reservations.
|
Has and belongs to many (One source has and belongs to many targets)
| This is a "many-to-many" relation. Multiple objects of the source model are related to multiple objects of the target model.
The source model (User) obtains a new collection variable that stores multiple objects of the target model (User.Roles).
The target model (role) obtains a new collection variable that stores multiple objects of the source model (Role.Users). | Examples:
Author can write multiple Books but a Book can also be written by many Authors.
User can have multiple Roles and those Roles can also be applied to multiple other Users.
|
Once you create a Belong to relation (many Books belong to one Author), an inversed Has many relation is made on the other end (one Author has many Books).
There is also one extra type of relation that involves only a single model creating a self-referencing relation. For example, an Employee model that represents the employees in an organization. Various employees can be assigned as Roles of supervisors or team leaders for multiple employees.
Displaying relations
You can open a detailed view of the relations between models from either the canvas view or the models overview list.
Canvas view: relation details are displayed as shown by the lines connecting the various models:
Model overview list: view the relations by heading to a single model and opening Relations tab. For example, Data models > Article > Relations:
Showing multiple relations between models
Where a model has multiple relations, a count of the number of relations is shown in the canvas view. Click on the relation line to see the overview of the relation types.
Select one of the relation types from the list displayed. The relation details are detailed in the pane that is opened.
Want to know more about some development standards and best practices for using models in your application? Check out this article!