Skip to main content
Version: 2.3.0

Models

It's possible to map serializable models to tables in your database. To do this, add the table key to your yaml file:

class: Company
table: company
fields:
name: String

When the table keyword is added to the model, the serverpod generate command will generate new methods for interacting with the database. The addition of the keyword will also be detected by the serverpod create-migration command that will generate the necessary migrations needed to update the database.

info

When you add a table to a serializable class, Serverpod will automatically add an id field of type int? to the class. You should not define this field yourself. The id is set when you interact with an object stored in the database.

Non persistent fields

You can opt out of creating a column in the database for a specific field by using the !persist keyword.

class: Company
table: company
fields:
name: String, !persist

All fields are persisted by default and have an implicit persist set on each field.

Data representation

Storing a field with a primitive / core dart type will be handled as its respective type. However, if you use a complex type, such as another model, a List, or a Map, these will be stored as a json object in the database.

class: Company
table: company
fields:
address: Address # Stored as a json column

This means that each row has its own copy of the nested object that needs to be updated individually. If you instead want to reference the same object from multiple different tables, you can use the relation keyword.

This creates a database relation between two tables and always keeps the data in sync.

class: Company
table: company
fields:
address: Address?, relation

For a complete guide on how to work with relations see the relation section.