Create an Entity to Persist Data
In OutSystems, a database table is an Entity, and the table columns are Entity Attributes.
To create an Entity you need to:
- Open the Entity Diagram created by default in your application.
- Right-click anywhere on the canvas and select 'Add Entity'.
- In the window that pops up:
- Give a name to the entity.
- The entity already has the Id attribute created as Entity Identifier (primary key). Add the other attributes.
- Add indexes if necessary.
Alternatively, you can create entities in the Data tab:
- Right-click on the Entities folder and select 'Add Entity'.
It's a quicker way, but with access to less information about the entity. - Right-click on the Entities folder and select 'Import New Entities from Excel...'.
It's useful to create an entity and bootstrap data for it.
In mobile applications, you can create entities to store information in the device's local storage. This typically happens when end users need to take the data offline.
Example
We have an application called Go Out, that allows you to read and write reviews of places you go to, for instance restaurants. Let's see how to create the table that stores the information about these places.
-
In the Data tab, under Entity Diagrams, open the
GoOutDataModel
diagram. -
Right-click and choose 'Add Entity to Database'.
-
Set the name of the entity to
Place
. OutSystems creates anId
attribute with data typeLong Integer
, set as AutoNumber. -
Use the
New button to create the attributes:
- Create the
Name
attribute. By default the data type is set toText
and the length is50
, change it to100
; - Make the attribute mandatory by setting the Is Mandatory property to
Yes
. - Create the
Address
attribute as a mandatory Text attribute with 200 characters of length. - Create the
PhoneNumber
attribute. The data type of the attribute changes toPhone Number
. Leave this attribute optional. - Create the
Latitude
andLongitude
attributes as optional Decimal attributes. Use the default length (37) and decimals (8) to define the precision of the numbers stored in the database.
- Create the
-
Publish your application.
When you publish your application, OutSystems creates the database table that corresponds to the Place entity.