Create Cake and CakesBakers
This commit is contained in:
parent
c76557135e
commit
0011f5e7d1
107
tests/bakery_chain/cake.rs
Normal file
107
tests/bakery_chain/cake.rs
Normal file
@ -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<i32>,
|
||||||
|
pub lineitem_id: Option<i32>,
|
||||||
|
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<super::bakery::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::Bakery.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::baker::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
super::cakes_bakers::Relation::Baker.def()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn via() -> Option<RelationDef> {
|
||||||
|
Some(super::cakes_bakers::Relation::Cake.def().rev())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Model {
|
||||||
|
pub fn find_bakers(&self) -> Select<super::baker::Entity> {
|
||||||
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
68
tests/bakery_chain/cakes_bakers.rs
Normal file
68
tests/bakery_chain/cakes_bakers.rs
Normal file
@ -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 {}
|
@ -1,5 +1,7 @@
|
|||||||
pub mod baker;
|
pub mod baker;
|
||||||
pub mod bakery;
|
pub mod bakery;
|
||||||
|
pub mod cake;
|
||||||
|
pub mod cakes_bakers;
|
||||||
pub mod customer;
|
pub mod customer;
|
||||||
pub mod lineitem;
|
pub mod lineitem;
|
||||||
pub mod order;
|
pub mod order;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use sea_orm::{sea_query, DbConn, ExecErr, ExecResult};
|
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;
|
pub mod bakery_chain;
|
||||||
mod setup;
|
mod setup;
|
||||||
@ -18,6 +18,7 @@ async fn setup_schema(db: &DbConn) {
|
|||||||
assert!(create_customer_table(db).await.is_ok());
|
assert!(create_customer_table(db).await.is_ok());
|
||||||
assert!(create_order_table(db).await.is_ok());
|
assert!(create_order_table(db).await.is_ok());
|
||||||
assert!(create_lineitem_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<ExecResult, ExecErr> {
|
async fn create_bakery_table(db: &DbConn) -> Result<ExecResult, ExecErr> {
|
||||||
@ -156,3 +157,27 @@ async fn create_lineitem_table(db: &DbConn) -> Result<ExecResult, ExecErr> {
|
|||||||
|
|
||||||
db.execute(stmt.into()).await
|
db.execute(stmt.into()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn create_cakes_bakers_table(db: &DbConn) -> Result<ExecResult, ExecErr> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user