Topics Map > Models

LCS - Create a relationship between models

This document will explain what a relationship is and how to set one up in Betty Blocks.

Introduction to Creating a Relationship Between Models

A relationship between models tells how they are connected to each other. Each model has a relation to the other, but you will only need to specify the relationship in one direction. The other direction is then automatically set up for you. Each defined relationship is bidirectional (meaning it is accessible from both models). 

There are three types of relationships available: BELONGS TO (many to one), HAS MANY (one to many), and HAS AND BELONGS TO MANY (many to many).

HAS MANY: It’s often easiest to use the HAS MANY relationship and think about things from the top down. If I go to an online shopping site, I am a customer who then places an order. I can place many different orders on that site. I can also purchase many items on each order. In other words, each customer can have many orders and each order can have many items. To set this up, add a relationship in the customer model that is HAS MANY to the order Model. Then go to the order model and add a relationship that is HAS MANY to the Item model. That’s all you need to do and you are done.

BELONGS TO: You can think of the previous example with customers, orders, and items from the bottom up as well. An item belongs to a single order. An order belongs to a single customer. It doesn’t matter which way you think about it. Once you set a relationship then the opposite one will automatically be set up for you.

HAS AND BELONGS TO MANY: The HAS AND BELONGS TO MANY relationship is described in a separate, advanced article, as are the relationships for bridge models. (Bridge models provide a means of relating two models, plus including a third model to hold additional data pertaining to the relationship.)

Tip for Professional Developers

Where are the foreign keys? There are no foreign keys in Betty Blocks, at least not any that you can see. Do not create any foreign keys. This can be a bit disconcerting, but trust that Betty Blocks sets things up correctly behind the scenes.

Use the BELONGS TO relationship anytime that you would normally put in a foreign key. That can be a quick way to think about things. For example, in the order model you would normally put in a foreign key for customer_id so you know which customer the order belongs to. That immediately tells you that the order model has a BELONGS To relationship to the customer model.

Creating a Relationship Between Models

Select a model you would like to create a relationship from. In this case we are going to create a One To Many relationship between the Customer and the Order models. 

  1. Select the Relations tab of the Customer and then select the New Relation button on the upper right side of the page. 

    Model Page

  2. A panel will slide out that looks like the following image. You will update the type and associated model in the next steps.

    Relationship Options

  3. Specify the Relationship type. In this case it's Has many.

    Has Many Relationship

  4. Select the model to which the customer relates. In this case the customer has many orders so we put in Order as the model.

    Model that the relationship relates to

  5. A name for the relationship is put in there as a default. You can just leave it or change it if you like.

    Relationship Name

  6. Select the Save button and you are done.

You can view the relationships for any model anytime by selecting the Relations tab.

Model with new relationship



Keywordsmodel, relationship, belongs to, has many, has and belongs to many   Doc ID133811
OwnerMatt M.GroupLow Code Solutions
Created2024-01-04 08:42:41Updated2024-01-19 14:34:55
SitesDoIT Enterprise Business Systems - Low Code Solutions
Feedback  0   0