|
| 1 | +use sea_orm::entity::prelude::*; |
| 2 | + |
| 3 | +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] |
| 4 | +#[sea_orm(table_name = "filling")] |
| 5 | +pub struct Entity; |
| 6 | + |
| 7 | +#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] |
| 8 | +pub struct Model { |
| 9 | + pub id: i32, |
| 10 | + pub name: String, |
| 11 | + pub vendor_id: Option<i32>, |
| 12 | + #[sea_orm(ignore)] |
| 13 | + pub ignored_attr: i32, |
| 14 | +} |
| 15 | + |
| 16 | +// If your column names are not in snake-case, derive `DeriveCustomColumn` here. |
| 17 | +#[derive(Copy, Clone, Debug, EnumIter, DeriveCustomColumn)] |
| 18 | +pub enum Column { |
| 19 | + Id, |
| 20 | + Name, |
| 21 | + VendorId, |
| 22 | +} |
| 23 | + |
| 24 | +// Then, customize each column names here. |
| 25 | +impl IdenStatic for Column { |
| 26 | + fn as_str(&self) -> &str { |
| 27 | + match self { |
| 28 | + // Override column names |
| 29 | + Self::Id => "id", |
| 30 | + // Leave all other columns using default snake-case values |
| 31 | + _ => self.default_as_str(), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] |
| 37 | +pub enum PrimaryKey { |
| 38 | + Id, |
| 39 | +} |
| 40 | + |
| 41 | +impl PrimaryKeyTrait for PrimaryKey { |
| 42 | + type ValueType = i32; |
| 43 | + |
| 44 | + fn auto_increment() -> bool { |
| 45 | + true |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Copy, Clone, Debug, EnumIter)] |
| 50 | +pub enum Relation {} |
| 51 | + |
| 52 | +impl ColumnTrait for Column { |
| 53 | + type EntityName = Entity; |
| 54 | + |
| 55 | + fn def(&self) -> ColumnDef { |
| 56 | + match self { |
| 57 | + Self::Id => ColumnType::Integer.def(), |
| 58 | + Self::Name => ColumnType::String(None).def(), |
| 59 | + Self::VendorId => ColumnType::Integer.def().nullable(), |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl RelationTrait for Relation { |
| 65 | + fn def(&self) -> RelationDef { |
| 66 | + panic!("No RelationDef") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Related<super::cake::Entity> for Entity { |
| 71 | + fn to() -> RelationDef { |
| 72 | + super::cake_filling::Relation::Cake.def() |
| 73 | + } |
| 74 | + |
| 75 | + fn via() -> Option<RelationDef> { |
| 76 | + Some(super::cake_filling::Relation::Filling.def().rev()) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl ActiveModelBehavior for ActiveModel {} |
0 commit comments