Inside our project, we run LB model category. It takes the model name from the comment line, so we can accept the defaults. We use the default in-memory DB data source, the default persistent model base class, accept to expose the category via REST, and accept the default inaudible form, and we accept to create a common model.
Our category model has been created, and we can now add the model properties. We add one property called name. We select type string, set it to be required, and at no default value. As this is the only property we need, we hit enter to quit the dialog.
When we look at our project, we see that the category got defined at model config, and that the category definition and its class got created in common models. Now, let's create a relation between the category and the product model.
First, we run LB property to add a property to the product model. We select the product model, use category ID as the property name, select type number. We don't make it required, and we don't use there default value.
Then we run LB relation, we select a category model, select relation type has many, and select product as the second model. The name of the relationship will be products. Category has many products. It's a foreign key, we set the category ID, and we will not use a through model.
We run LB relation a second time. This time, the first model we select is the product model. The relationship type is belongs to, and we select the category as the second model. The name of the relation will be category, so product belongs to category.
It's a foreign key, we set the category ID, and this type of relation will not ask for a through model. When we look at our products model, we see that a new endpoint got added. This endpoint can be used to interact with the new relationship.
In the category models, we see a few endpoints that allow to interact with the product's relationship. Let's create some data to see how this works. Let's add a category, drinks and food. When we look at our endpoint, we see we have the two category in place.
In the product model, we add coffee and tea, and add it to the first category. We add lemon cake and cheesecake, and add it to the second category. When we look at our products endpoint, we see that the categories got added.
To see the relation working, we can go to the category model, open the get endpoint for categories ID products, and enter a category ID. We will see that this will return only the products in this category. When we change the ID, we see that we get the products of the other category.
We can also view which category is related to a product. To do so, we go to the get products ID category endpoint, and enter the ID. We will see that this product belongs in category drinks, and we see that this product belongs in the category food.
Now, if we look at our project, we see how the relations are defined. In the categories model, we find the new relation under the projects key. It defines the type of relation, the model it relates to, and the foreign key. In the product model, we can see that the category ID property got added, and that the relation called category is also defined.