添加crate
sea-orm = { version = "1.0.1", features = [ "sqlx-postgres", "runtime-tokio-rustls", "macros" ] }
安装cli
cargo install sea-orm-cli
初始化,将会生成migration
文件夹
迁移可以在开发时随时回滚到数据库初始状态,也可以让其他人通过运行命令来初始化一样的开发环境,还可以保留不同版本的数据库设计,随时初始化不同版本的数据库,避免手动导入sql、手动删除开发测试数据
sea-orm-cli migrate init
将migration
添加到工作区,根目录Cargo.toml
[workspace]
members = [".","migration"]
取消驱动的注释migration\Cargo.toml
[dependencies.sea-orm-migration]
version = "1.0.0"
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 https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
# e.g.
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
"sqlx-postgres", # `DATABASE_DRIVER` feature
]
添加或修改你需要的字段,这里定义了一个Post
表,id
、title
、text
三个字段
#[derive(DeriveIden)]
enum Post {
Table,// 存储表名但不成为数据库字段
Id,
Title,
#[sea_orm(iden = "full_text")] // 重命名数据库里的字段名
Text,
}
编写建表命令,所有类型和约束都已函数的方式添加
todo!()
用于提示未完成的部分,使用todo!()
会panic,迁移前请删除
use sea_orm_migration::{prelude::*, schema::*};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]//允许为自定义结构体实现异步MigrationTrait
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()// 建表
.table(Post::Table)// 表名
.if_not_exists()// 不存在则创建
.col(pk_auto(Post::Id))// 主键自增
.col(string(Post::Title))// string类型的Title
.col(string(Post::Text))// string类型的Text
.to_owned(),// 创建一个拥有所有权的副本
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Post::Table).to_owned())
.await
}
}
up应用 down回滚
SeaQuery
其他定义https://www.sea-ql.org/SeaORM/docs/migration/writing-migration/
还可以创建多个表
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 创建用户表
manager.create_table(
Table::create()
.table(User::Table)
.if_not_exists()
.col(ColumnDef::new(User::Id).integer().not_null().auto_increment().primary_key())
.col(ColumnDef::new(User::Username).string().not_null())
.col(ColumnDef::new(User::Email).string().not_null().unique_key())
.to_owned()
).await?;
// 创建订单表
manager.create_table(
Table::create()
.table(Order::Table)
.if_not_exists()
.col(ColumnDef::new(Order::Id).integer().not_null().auto_increment().primary_key())
.col(ColumnDef::new(Order::UserId).integer().not_null())
.col(ColumnDef::new(Order::TotalPrice).decimal().not_null())
.to_owned()
).await?;
// 创建产品表
manager.create_table(
Table::create()
.table(Product::Table)
.if_not_exists()
.col(
ColumnDef::new(Product::Id).integer().not_null().auto_increment().primary_key()
)
.col(ColumnDef::new(Product::Name).string().not_null())
.col(ColumnDef::new(Product::Price).decimal().not_null())
.to_owned()
).await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 删除产品表
manager.drop_table(Table::drop().table(Product::Table).to_owned()).await?;
// 删除订单表
manager.drop_table(Table::drop().table(Order::Table).to_owned()).await?;
// 删除用户表
manager.drop_table(Table::drop().table(User::Table).to_owned()).await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum User {
Table,
Id,
Username,
Email,
}
#[derive(DeriveIden)]
enum Order {
Table,
Id,
UserId,
TotalPrice,
}
#[derive(DeriveIden)]
enum Product {
Table,
Id,
Name,
Price,
}
在根目录创建.env
文件,添加以下内容,修改username:password@host
DATABASE_URL=postgres://username:password@host:5432/database
# 例如
DATABASE_URL=postgres://postgres:root123456@localhost:5432/postgres
运行迁移
迁移将在 Postgres 中以原子方式执行,失败则回滚,MySQL 和 SQLite 不支持原子迁移
- 迁移,除了会创建自定义的表外,还有一个
seaql_migrations
的版本信息表
其他目录通过
-d
来指定sea-orm-cli migrate COMMAND -d ./other/migration/dir
sea-orm-cli migrate up
- 回滚
sea-orm-cli migrate down
- 检查迁移的状态
sea-orm-cli migrate status
- 删除所有表重新迁移
会删除整个数据库的表,不仅仅是迁移定义的表
sea-orm-cli migrate fresh
- 回滚所有已应用的迁移,然后重新应用所有迁移
sea-orm-cli migrate refresh
- 回滚所有已应用的迁移
sea-orm-cli migrate reset
创建实体
指定在entity/src
下创建实体
sea-orm-cli generate entity -o entity/src
常用参数
-
-o
指定输出目录 -
--with-serde
序列化与反序列化,指定值none
、serialize
、deserialize
、both
,默认none
--serde-skip-deserializing-primary-key
生成主键字段标记为的实体模型#[serde(skip_deserializing)]
--serde-skip-hidden-column
:生成带有隐藏列(列名以 开头_
)字段的实体模型,标记为#[serde(skip)]