diff --git a/tests/bakery_chain/cake.rs b/tests/bakery_chain/cake.rs new file mode 100644 index 00000000..976d8acb --- /dev/null +++ b/tests/bakery_chain/cake.rs @@ -0,0 +1,107 @@ +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn table_name(&self) -> &str { + "cake" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] +pub struct Model { + pub id: i32, + pub name: String, + pub price: f32, + pub bakery_id: Option, + pub lineitem_id: Option, + pub best_before: String, + pub produced_at: String, + pub gluten_free: bool, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id, + Name, + Price, + BakeryId, + LineitemId, + BestBefore, + ProducedAt, + GlutenFree, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id, +} + +impl PrimaryKeyTrait for PrimaryKey { + fn auto_increment() -> bool { + true + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Bakery, + Lineitem, +} + +impl ColumnTrait for Column { + type EntityName = Entity; + + fn def(&self) -> ColumnDef { + match self { + Self::Id => ColumnType::Integer.def(), + Self::Name => ColumnType::String(None).def(), + Self::Price => ColumnType::Money(Some((19, 4))).def(), + Self::BakeryId => ColumnType::Integer.def(), + Self::LineitemId => ColumnType::Integer.def(), + Self::BestBefore => ColumnType::Date.def(), + Self::ProducedAt => ColumnType::Timestamp.def(), + Self::GlutenFree => ColumnType::Boolean.def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Bakery => Entity::belongs_to(super::bakery::Entity) + .from(Column::BakeryId) + .to(super::bakery::Column::Id) + .into(), + Self::Lineitem => Entity::belongs_to(super::lineitem::Entity) + .from(Column::LineitemId) + .to(super::lineitem::Column::Id) + .into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Bakery.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + super::cakes_bakers::Relation::Baker.def() + } + + fn via() -> Option { + Some(super::cakes_bakers::Relation::Cake.def().rev()) + } +} + +impl Model { + pub fn find_bakers(&self) -> Select { + Entity::find_related().belongs_to::(self) + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/tests/bakery_chain/cakes_bakers.rs b/tests/bakery_chain/cakes_bakers.rs new file mode 100644 index 00000000..8106bbdf --- /dev/null +++ b/tests/bakery_chain/cakes_bakers.rs @@ -0,0 +1,68 @@ +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn table_name(&self) -> &str { + "cakes_bakers" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] +pub struct Model { + pub cake_id: i32, + pub baker_id: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + CakeId, + BakerId, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + CakeId, + BakerId, +} + +impl PrimaryKeyTrait for PrimaryKey { + fn auto_increment() -> bool { + false + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Cake, + Baker, +} + +impl ColumnTrait for Column { + type EntityName = Entity; + + fn def(&self) -> ColumnDef { + match self { + Self::CakeId => ColumnType::Integer.def(), + Self::BakerId => ColumnType::Integer.def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Cake => Entity::belongs_to(super::cake::Entity) + .from(Column::CakeId) + .to(super::cake::Column::Id) + .into(), + Self::Baker => Entity::belongs_to(super::baker::Entity) + .from(Column::BakerId) + .to(super::baker::Column::Id) + .into(), + } + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/tests/bakery_chain/mod.rs b/tests/bakery_chain/mod.rs index 4c557e26..e0a4100e 100644 --- a/tests/bakery_chain/mod.rs +++ b/tests/bakery_chain/mod.rs @@ -1,5 +1,7 @@ pub mod baker; pub mod bakery; +pub mod cake; +pub mod cakes_bakers; pub mod customer; pub mod lineitem; pub mod order; diff --git a/tests/bakery_chain_tests.rs b/tests/bakery_chain_tests.rs index afcaa1fa..c8631e6c 100644 --- a/tests/bakery_chain_tests.rs +++ b/tests/bakery_chain_tests.rs @@ -1,5 +1,5 @@ use sea_orm::{sea_query, DbConn, ExecErr, ExecResult}; -use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, SqliteQueryBuilder}; +use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Index, SqliteQueryBuilder}; pub mod bakery_chain; mod setup; @@ -18,6 +18,7 @@ async fn setup_schema(db: &DbConn) { assert!(create_customer_table(db).await.is_ok()); assert!(create_order_table(db).await.is_ok()); assert!(create_lineitem_table(db).await.is_ok()); + assert!(create_cakes_bakers_table(db).await.is_ok()); } async fn create_bakery_table(db: &DbConn) -> Result { @@ -156,3 +157,27 @@ async fn create_lineitem_table(db: &DbConn) -> Result { db.execute(stmt.into()).await } + +async fn create_cakes_bakers_table(db: &DbConn) -> Result { + let stmt = sea_query::Table::create() + .table(cakes_bakers::Entity) + .if_not_exists() + .col( + ColumnDef::new(cakes_bakers::Column::CakeId) + .integer() + .not_null(), + ) + .col( + ColumnDef::new(cakes_bakers::Column::BakerId) + .integer() + .not_null(), + ) + .primary_key( + Index::create() + .col(cakes_bakers::Column::CakeId) + .col(cakes_bakers::Column::BakerId), + ) + .build(SqliteQueryBuilder); + + db.execute(stmt.into()).await +}