You can define a database index in your data model to enforce uniqueness of table attributes across multiple rows or to make searches quicker using those attributes as filters. In OutSystems, you can model a database index in the entity element.
To create an index for an entity:
- Open the entity by double-clicking on it or selecting the Indexes property;
- Go to the Indexes tab and create a new index;
- Add the attributes you want to include in the index;
- If you want to have unique values for the selected attributes, set the Unique property to
- To apply the index created in the database, publish the application.
In the GoOutWeb application, an application to find and review interesting places, we want to ensure that an end-user cannot give more than one review to a place in the same day.
To create a unique index in the Review entity to ensure this restriction:
- Open the Review entity;
- In the Indexes tab, add a new index and name it
- Add the attributes: PlaceId, UserId, and Date. This will ensure that the same value for these attributes cannot be repeated in other records of Review table;
- Set the index to unique;
- Publish the module. While publishing, OutSystems creates the index in the database.
From now on, when an end-user tries to save the second review for the same place in the same day, OutSystems raises a database exception.