Create a One-to-Many Relationship
When modeling data, it is sometimes necessary to create one-to-many relationships between entities. An example is when an Order (parent entity) can have many Items (child entity). This is typically implemented with a foreign key in the child records, that is the identifier of the parent record.
In OutSystems, to create a one-to-many relationship between two entities, do the following:
- Select the entity with the child records (e. g.:
Item); - Add a new attribute that holds the identifier of the parent entity (e. g.: identifier of the
Orderentity). This attribute will be the foreign key.
In OutSystems, having an identifier attribute pointing to another entity creates automatically a relationship. You can see the relationships between entities if you have them in the same Entity Diagram.
Example
We have an application called GoOutWeb, that enables users to create reviews for places like restaurants, bars, etc. We want to store the fact that a certain Review was created about a specific Place record.
To achieve this, follow these steps:
- Go to the Data tab;
- Add an attribute to the
Reviewentity. Name itPlaceId. OutSystems automatically infers that its data type isPlace Identifier. It also creates the relationship between the two entities. - In the action that creates
Reviewrecords, make sure that thePlaceIdattribute holds the identifier of an existingPlacerecord that theReviewis created for; - Publish the application.
As a result, you have a one-to-many relationship between the Place and Review entities:
