Skip to main content
Version: 0.12.x

Setting Up Migration

We need your help! 📝

Thank you for reading this documentation. While we have you, would you spare a few minutes into completing our SeaQL Community Survey? This is essential for the continued development and maintenance of SeaORM.

If you are starting from a fresh database, it's better to version control your database schema. SeaORM ships with a migration tool, allowing you to write migrations in SeaQuery or SQL.

If you already have a database with tables and data, you can skip this chapter and move on to generating SeaORM entities.

Migration Table

A table will be created in your database to keep track of the applied migrations. It will be created automatically when you run the migration.

By default, it will be named `seaql_migrations`. You can also use a custom name for your migration table.
impl MigratorTrait for Migrator {
// Override the name of migration table
fn migration_table_name() -> sea_orm::DynIden {

Creating Migration Directory

First, install sea-orm-cli with cargo.

cargo install sea-orm-cli

Then, setup the migration directory by executing sea-orm-cli migrate init.

# Setup the migration directory in `./migration`
$ sea-orm-cli migrate init
Initializing migration directory...
Creating file `./migration/src/`
Creating file `./migration/src/`
Creating file `./migration/src/`
Creating file `./migration/Cargo.toml`
Creating file `./migration/`

# If you want to setup the migration directory in else where
$ sea-orm-cli migrate init -d ./other/migration/dir

You should have a migration directory with a structure like below.

├── Cargo.toml
└── src
├── # Migrator API, for integration
├── # A sample migration file
└── # Migrator CLI, for running manually

Note that if you setup the migration directory directly within a Git repository, a .gitignore file will also be created.

Workspace Structure

It is recommended to structure your cargo workspace as follows to share SeaORM entities between the app crate and the migration crate. Checkout the integration examples for demonstration.

Migration Crate

Import the sea-orm-migration and async-std crate.

async-std = { version = "1", features = ["attributes", "tokio1"] }

version = "0.12"
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
# View the list of supported features at
# e.g.
# "runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
# "sqlx-postgres", # `DATABASE_DRIVER` feature

Let's write a migration. Detailed instructions in the next section.

use sea_orm_migration::prelude::*;

pub struct Migration;

impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts

Entity Crate

Create an entity crate in your root workspace.

You don't have SeaORM entities defined?

You can create an entity crate without any entity files. Then, write the migration and run it to create tables in the database. Finally, generate SeaORM entities with sea-orm-cli and output the entity files to entity/src/entities folder.

After generating the entity files, you can re-export the generated entities by adding following lines in entity/src/

mod entities;
pub use entities::*;
├── Cargo.toml # Include SeaORM dependency
└── src
├── # Re-export SeaORM and entities
└── # Define the `post` entity

Specify SeaORM dependency.

sea-orm = { version = "0.12" }

App Crate

This is where the application logic goes.

Create a workspace that contains app, entity and migration crates and import the entity crate into the app crate.

If we want to bundle the migration utility as part of your app, you'd also want to import the migration crate.

members = [".", "entity", "migration"]

entity = { path = "entity" }
migration = { path = "migration" } # depends on your needs

sea-orm = { version = "0.12", features = [..] }

In your app, you can then run the migrator on startup.

use migration::{Migrator, MigratorTrait};

let connection = sea_orm::Database::connect(&database_url).await?;
Migrator::up(&connection, None).await?;