Data Models Designer
Creation of database using data models designer
Database design
It's time to design your own database. To do this, go to the tab Database on the left panel.
Data models
The data in the database is stored in the form of special tables (models). And you can notice that we already have one model. It is part of the authorization module and is included in every project by default. Thanks to it, new users of the application are created and existing ones are managed. But we will not dwell on its study now, we will create our own model.
Imagine that we are developing a map service. Let's create a model that contains information about countries. To create it, you need to right-click in an empty area of the canvas and select Create empty model.
To create, we only need to specify the name of the model. We will deal with auto-generation of endpoints and user interface elements in further modules of the course.
Model fields
Please note that immediately after creation, the model already contains 4 fields. These are system fields, the presence of which greatly simplifies the initial creation and further use of the model.
ID (integer) - Unique identifier, primary key. It is automatically created for each new entry in the table and is intended to ensure that there are no duplicates. It is by ID that you can uniquely identify a record in a table. Its value starts at 1 and automatically increases by 1 for each new entry.
CreatedAt (datetime) - The time the record was created in the table.
UpdatedAt (datetime) - The time the entry was last modified.
DeletedAt (datetime) - The time the entry was deleted. Of course, only if soft-removal was used. That is, such a deletion, when the record is only marked as deleted and filtered by requests for access to it, but at the same time physically remains in the table. This is different from bulk deletion, which actually deletes the data completely.
In addition to the system ones, it would be wise to add custom fields to the created model. Suppose we want to see the name of the country and some description with information about it.
Choosing a field type shouldn't be a problem. String is suitable for the name, and Text for the informational description.
In addition, four more switches are available:
Multiple values (Array) - use arrays instead of single entries.
Not null - the specified field cannot be empty, it must always contain data.
Unique - the value of the field must be unique, in this model there cannot be two records whose values of this field are the same.
Index - indicates that a special index will be created for this field in order to speed up the search.
In general, it's only right to check marks if it's really necessary. For example, we could mark Not null and Unique for country names, assuming that there can't be a country without a name, or two countries with the same name. However, it is a good idea to control this at the stage of creating the logic of the application, and not put restrictions on the database itself.
Similarly, create a table with information about cities. Think about what data fields it can contain, what type these fields are.
Data models relations
The data in the database does not exist on its own, in the form of scattered tables. They are related to each other in a certain way. The key to developing a data model is to define these relationships and build relationships.
To establish such links, it is necessary to draw a line with the mouse from the border of one model to another. In our example, we know for sure that each city is located in some country, so we can create a link from country to city.
There are 3 different types of connections:
One-to-one (has one). Each record in the table is mapped to one record from the associated table (this is also true in reverse). A simple example is a person and their passport. We can always be sure that this connection is unique. A passport can only have one holder, and each person can have only one valid passport.
One-to-many (has many). Each record in one table can have many records in another table. Our database is a similar example. A country can have many different cities, but each city can belong to only one country. This is the connection we will make.
Many-to-many. A relationship in which multiple records from one table can correspond to multiple records from another. A simple example is the relationship between teachers and students. Each teacher can teach many students, just as each student can learn from many different teachers.