Use it like a derive macro
use sea_orm_macro::AutoColumn;
#[derive(AutoColumn)]
struct Model {
id: u64,
name: String,
}
this will autogenerate the Column enum, like this
#[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)]
enum Column {
Id,
Name,
}
impl sea_orm::prelude::ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> sea_orm::prelude::ColumnDef {
match self {
Column::Id => sea_orm::prelude::ColumnType::Integer.def(),
Column::Name => sea_orm::prelude::ColumnType::String(None).def(),
}
}
}
To autogenerate the PrimaryKey enum too, simply add the attribute like this
use sea_orm_macro::AutoColumn;
#[derive(AutoColumn)]
struct Model {
#[auto_column(primary_key)]
id: u64,
name: String,
}
this will autogenerate the PrimaryKey enum, like this
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
true
}
}
if you have more than one primary key, the auto_increment method will automatically return false
use sea_orm_macro::AutoColumn;
#[derive(AutoColumn)]
struct Model {
#[auto_column(primary_key)]
id1: u64,
#[auto_column(primary_key)]
id2: u64,
}
this will autogenerate the PrimaryKey enum, like this
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id1,
Id2,
}
impl PrimaryKeyTrait for PrimaryKey {
fn auto_increment() -> bool {
false
}
}
If your struct uses a non-basic type, you'll have to specify the SQL type for that column:
use sea_orm_macro::AutoColumn;
#[derive(AutoColumn)]
struct Model {
#[auto_column(primary_key)]
id: u64,
#[auto_column(type = "String(Some(255))")]
name: MyName,
#[auto_column(type = "Char(Some(1))", nullable)]
admin: Option<MyBool>,
}
this will autogenerate the Column enum, like this
#[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)]
enum Column {
Id,
Name,
Admin,
}
impl sea_orm::prelude::ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> sea_orm::prelude::ColumnDef {
match self {
Column::Id => sea_orm::prelude::ColumnType::Integer.def(),
Column::Name => sea_orm::prelude::ColumnType::String(Some(2555)).def()
Column::Admin => sea_orm::prelude::ColumnType::Char(Some(1)).def().null(),
}
}
}
You can also use it to override the default typing:
use sea_orm_macro::AutoColumn;
#[derive(AutoColumn)]
struct Model {
#[auto_column(primary_key)]
id: u64,
#[auto_column(type = "String(Some(255))")]
name: String,
}
this will autogenerate the Column enum, like this
#[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)]
enum Column {
Id,
Name,
}
impl sea_orm::prelude::ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> sea_orm::prelude::ColumnDef {
match self {
Column::Id => sea_orm::prelude::ColumnType::Integer.def(),
Column::Name => sea_orm::prelude::ColumnType::String(Some(255)).def(),
}
}
}
The default type mapping is:
Rust | SQL |
---|---|
char | Char(None) |
String | String(None) |
&str | String(None) |
u8" | TinyInteger |
i8 | TinyInteger |
u16 | SmallInteger |
i16 | SmallInteger |
u32 | Integer |
u64 | Integer |
i32 | Integer |
i64 | Integer |
u128 | BigInteger |
i128 | BigInteger |
f32 | Float |
f64 | Double |
bool | Boolean |
NaiveDate | Date |
NaiveTime | Time |
NaiveDateTime | DateTime |
Uuid | Uuid |
Decimal | BigInteger |