Skip to main content

Defining Models

Models in Sequelize Dart are regular Dart classes annotated with @Table and various column annotations. Here's the basic structure:

import 'package:sequelize_orm/sequelize_orm.dart';
import 'package:sequelize_orm_annotations/sequelize_orm_annotations.dart'; // Optional if exported by main package

part 'users.model.g.dart';

(tableName: 'users')
class User {
()
()
DataType id = DataType.INTEGER;

('first_name')
DataType firstName = DataType.STRING;

('last_name')
DataType lastName = DataType.STRING;

// Use @AllowNull or @NotNull
()
DataType bio = DataType.TEXT;

static UserModel get model => UserModel();
}

Table Configuration

The @Table annotation allows you to configure the table definition.

Basic Options

OptionTypeDescription
tableNameStringThe name of the table in the database.
underscoredboolIf true, columns will use snake_case by default.
timestampsboolAdds createdAt and updatedAt timestamps. Default: true.
schemaString?The database schema (e.g. public).

All Options

Click to see all @Table options
OptionTypeDescription
tableNameStringThe table name (required).
omitNullbool?Don't persist null values. Default false.
noPrimaryKeybool?Disable automatic primary key. Default false.
timestampsbool?Enable timestamps. Default false.
paranoidbool?Soft deletes (requires timestamps). Default false.
underscoredbool?Snake_case columns. Default false.
hasTriggerbool?Indicates table has trigger. Default false.
freezeTableNamebool?Stop Sequelize from pluralizing table name. Default false.

| name | ModelNameOption? | Singular/Plural model names. | | modelName | String? | The name of the model. | | updatedAt | TimestampOption? | Custom name or disable updatedAt. | | deletedAt | TimestampOption? | Custom name or disable deletedAt. | | schema | String? | Database schema. | | schemaDelimiter | String? | Delimiter for schema. | | engine | String? | Storage engine (MySQL/MariaDB). | | charset | String? | Charset (MySQL/MariaDB). | | comment | String? | Table comment. | | collate | String? | Table collation. | | initialAutoIncrement | String? | Initial auto-increment value (MySQL). | | version | VersionOption? | Optimistic locking version. |

Column Annotations

Define your table columns using field annotations on DataType fields.

AnnotationDescription
@PrimaryKey()Marks the column as the primary key.
@AutoIncrement()Column auto-increments.
@ColumnName('name')Explicitly set the database column name.
@NotNull()Adds a NOT NULL constraint.
@AllowNull()Explicitly allows NULL.
@Default(value)Sets a default value.
@Unique()Adds a unique constraint.
@Index()Creates an index on the column.
@Comment('text')Adds a comment to the column.