Create Lineitem
This commit is contained in:
parent
1de4d0cd28
commit
c76557135e
80
tests/bakery_chain/lineitem.rs
Normal file
80
tests/bakery_chain/lineitem.rs
Normal file
@ -0,0 +1,80 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"lineitem"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
pub price: f32,
|
||||
pub quantity: i32,
|
||||
pub order_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
pub enum Column {
|
||||
Id,
|
||||
Price,
|
||||
Quantity,
|
||||
OrderId,
|
||||
}
|
||||
|
||||
#[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 {
|
||||
Order,
|
||||
}
|
||||
|
||||
impl ColumnTrait for Column {
|
||||
type EntityName = Entity;
|
||||
|
||||
fn def(&self) -> ColumnDef {
|
||||
match self {
|
||||
Self::Id => ColumnType::Integer.def(),
|
||||
Self::Price => ColumnType::Money(Some((19, 4))).def(),
|
||||
Self::Quantity => ColumnType::Integer.def(),
|
||||
Self::OrderId => ColumnType::Integer.def(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RelationTrait for Relation {
|
||||
fn def(&self) -> RelationDef {
|
||||
match self {
|
||||
Self::Order => Entity::belongs_to(super::order::Entity)
|
||||
.from(Column::Id)
|
||||
.to(super::order::Column::CustomerId)
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::order::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Order.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn find_orders(&self) -> Select<super::order::Entity> {
|
||||
Entity::find_related().belongs_to::<Entity>(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
@ -1,4 +1,5 @@
|
||||
pub mod baker;
|
||||
pub mod bakery;
|
||||
pub mod customer;
|
||||
pub mod lineitem;
|
||||
pub mod order;
|
||||
|
@ -42,6 +42,7 @@ impl PrimaryKeyTrait for PrimaryKey {
|
||||
pub enum Relation {
|
||||
Bakery,
|
||||
Customer,
|
||||
Lineitem,
|
||||
}
|
||||
|
||||
impl ColumnTrait for Column {
|
||||
@ -69,6 +70,7 @@ impl RelationTrait for Relation {
|
||||
.from(Column::CustomerId)
|
||||
.to(super::customer::Column::Id)
|
||||
.into(),
|
||||
Self::Lineitem => Entity::has_many(super::lineitem::Entity).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -85,4 +87,16 @@ impl Related<super::customer::Entity> for Entity {
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::lineitem::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Lineitem.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn find_lineitems(&self) -> Select<super::lineitem::Entity> {
|
||||
Entity::find_related().belongs_to::<Entity>(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
@ -17,6 +17,7 @@ async fn setup_schema(db: &DbConn) {
|
||||
assert!(create_baker_table(db).await.is_ok());
|
||||
assert!(create_customer_table(db).await.is_ok());
|
||||
assert!(create_order_table(db).await.is_ok());
|
||||
assert!(create_lineitem_table(db).await.is_ok());
|
||||
}
|
||||
|
||||
async fn create_bakery_table(db: &DbConn) -> Result<ExecResult, ExecErr> {
|
||||
@ -124,3 +125,34 @@ async fn create_order_table(db: &DbConn) -> Result<ExecResult, ExecErr> {
|
||||
|
||||
db.execute(stmt.into()).await
|
||||
}
|
||||
|
||||
async fn create_lineitem_table(db: &DbConn) -> Result<ExecResult, ExecErr> {
|
||||
let stmt = sea_query::Table::create()
|
||||
.table(lineitem::Entity)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(lineitem::Column::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(lineitem::Column::Price).float())
|
||||
.col(ColumnDef::new(lineitem::Column::Quantity).integer())
|
||||
.col(
|
||||
ColumnDef::new(lineitem::Column::OrderId)
|
||||
.integer()
|
||||
.not_null(),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("FK_lineitem_order")
|
||||
.from(lineitem::Entity, lineitem::Column::OrderId)
|
||||
.to(order::Entity, order::Column::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade)
|
||||
.on_update(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.build(SqliteQueryBuilder);
|
||||
|
||||
db.execute(stmt.into()).await
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user