Refactoring & Testing

This commit is contained in:
Billy Chan 2021-08-31 18:30:02 +08:00
parent d1d37fab07
commit 5fbc7b146a
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
11 changed files with 220 additions and 301 deletions

View File

@ -32,7 +32,7 @@ futures = { version = "^0.3" }
futures-util = { version = "^0.3" }
rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.1.1", optional = true }
sea-query = { version = "^0.15", features = ["thread-safe"] }
sea-query = { version = "^0.15", git = "https://github.com/SeaQL/sea-query.git", branch = "sea-orm/create-table-stmt", features = ["thread-safe"] }
sea-strum = { version = "^0.21", features = ["derive", "sea-orm"] }
serde = { version = "^1.0", features = ["derive"] }
sqlx = { version = "^0.5", optional = true }

View File

@ -55,21 +55,21 @@ pub trait EntityTrait: EntityName {
where
R: EntityTrait,
{
RelationBuilder::new(RelationType::HasOne, Self::default(), related)
RelationBuilder::new(RelationType::HasOne, Self::default(), related, false)
}
fn has_one<R>(_: R) -> RelationBuilder<Self, R>
where
R: EntityTrait + Related<Self>,
{
RelationBuilder::from_rel(RelationType::HasOne, R::to().rev())
RelationBuilder::from_rel(RelationType::HasOne, R::to().rev(), true)
}
fn has_many<R>(_: R) -> RelationBuilder<Self, R>
where
R: EntityTrait + Related<Self>,
{
RelationBuilder::from_rel(RelationType::HasMany, R::to().rev())
RelationBuilder::from_rel(RelationType::HasMany, R::to().rev(), true)
}
/// Construct select statement to find one / all models

View File

@ -26,7 +26,7 @@ pub enum ColumnType {
Timestamp,
Time,
Date,
Binary,
Binary(Option<u32>),
Boolean,
Money(Option<(u32, u32)>),
Json,
@ -276,7 +276,7 @@ impl From<ColumnType> for sea_query::ColumnType {
ColumnType::Timestamp => sea_query::ColumnType::Timestamp(None),
ColumnType::Time => sea_query::ColumnType::Time(None),
ColumnType::Date => sea_query::ColumnType::Date,
ColumnType::Binary => sea_query::ColumnType::Binary(None),
ColumnType::Binary(s) => sea_query::ColumnType::Binary(s),
ColumnType::Boolean => sea_query::ColumnType::Boolean,
ColumnType::Money(s) => sea_query::ColumnType::Money(s),
ColumnType::Json => sea_query::ColumnType::Json,
@ -307,7 +307,7 @@ impl From<sea_query::ColumnType> for ColumnType {
sea_query::ColumnType::Timestamp(_) => Self::Timestamp,
sea_query::ColumnType::Time(_) => Self::Time,
sea_query::ColumnType::Date => Self::Date,
sea_query::ColumnType::Binary(_) => Self::Binary,
sea_query::ColumnType::Binary(s) => Self::Binary(s),
sea_query::ColumnType::Boolean => Self::Boolean,
sea_query::ColumnType::Money(s) => Self::Money(s),
sea_query::ColumnType::Json => Self::Json,

View File

@ -34,6 +34,7 @@ pub struct RelationDef {
pub to_tbl: TableRef,
pub from_col: Identity,
pub to_col: Identity,
pub is_owner: bool,
}
pub struct RelationBuilder<E, R>
@ -47,6 +48,7 @@ where
to_tbl: TableRef,
from_col: Option<Identity>,
to_col: Option<Identity>,
is_owner: bool,
}
impl RelationDef {
@ -58,6 +60,7 @@ impl RelationDef {
to_tbl: self.from_tbl,
from_col: self.to_col,
to_col: self.from_col,
is_owner: !self.is_owner,
}
}
}
@ -67,7 +70,7 @@ where
E: EntityTrait,
R: EntityTrait,
{
pub(crate) fn new(rel_type: RelationType, from: E, to: R) -> Self {
pub(crate) fn new(rel_type: RelationType, from: E, to: R, is_owner: bool) -> Self {
Self {
entities: PhantomData,
rel_type,
@ -75,10 +78,11 @@ where
to_tbl: to.table_ref(),
from_col: None,
to_col: None,
is_owner,
}
}
pub(crate) fn from_rel(rel_type: RelationType, rel: RelationDef) -> Self {
pub(crate) fn from_rel(rel_type: RelationType, rel: RelationDef, is_owner: bool) -> Self {
Self {
entities: PhantomData,
rel_type,
@ -86,6 +90,7 @@ where
to_tbl: rel.to_tbl,
from_col: Some(rel.from_col),
to_col: Some(rel.to_col),
is_owner,
}
}
@ -118,6 +123,7 @@ where
to_tbl: b.to_tbl,
from_col: b.from_col.unwrap(),
to_col: b.to_col.unwrap(),
is_owner: b.is_owner,
}
}
}

View File

@ -1,54 +1,85 @@
use crate::entity::column::ColumnTrait;
use crate::entity::primary_key::PrimaryKeyToColumn;
use crate::entity::relation::RelationTrait;
use crate::{ColumnDef as Sea_Orm_Column_Def, EntityTrait, PrimaryKeyTrait, RelationDef};
use sea_query::{
Alias, ColumnDef as Sea_Query_Column_Def, ColumnType as Sea_Query_Column_Type,
ForeignKeyCreateStatement, IndexCreateStatement, SeaRc, TableCreateStatement, TableRef,
use crate::{
unpack_table_ref, ColumnTrait, EntityTrait, Identity, Iterable, PrimaryKeyToColumn,
PrimaryKeyTrait, RelationTrait,
};
pub use sea_strum::IntoEnumIterator;
pub trait CreateStatementOf {
fn create_table_statement_of<E>(entity: E) -> TableCreateStatement
where
use sea_query::{ColumnDef, ForeignKeyCreateStatement, Iden, Index, TableCreateStatement};
pub fn entity_to_table_create_statement<E>(entity: E) -> TableCreateStatement
where
E: EntityTrait,
{
{
let mut stmt = TableCreateStatement::new();
stmt.table(entity);
for column in E::Column::iter() {
let orm_column_def = column.def();
let types = orm_column_def.col_type.into();
let mut column_def = ColumnDef::new_with_type(column, types);
if !orm_column_def.null {
column_def.not_null();
}
if orm_column_def.unique {
column_def.unique_key();
}
for primary_key in E::PrimaryKey::iter() {
if column.to_string() == primary_key.into_column().to_string()
&& E::PrimaryKey::auto_increment()
{
column_def.auto_increment();
if E::PrimaryKey::iter().count() == 1 {
column_def.primary_key();
}
}
}
if orm_column_def.indexed {
stmt.index(
Index::create()
.name(&format!(
"idx-{}-{}",
entity.to_string(),
column.to_string()
))
.table(entity)
.col(column),
);
}
stmt.col(&mut column_def);
}
if E::PrimaryKey::iter().count() > 1 {
let mut idx_pk = Index::create();
for primary_key in E::PrimaryKey::iter() {
idx_pk.col(primary_key);
}
stmt.primary_key(idx_pk.name(&format!("pk-{}", entity.to_string())).primary());
}
for relation in E::Relation::iter() {
let relation = relation.def();
if relation.is_owner {
continue;
}
let mut foreign_key_stmt = ForeignKeyCreateStatement::new();
let relation_trait: RelationDef = relation.def();
// foreign_key_stmt.name("Temp");
match relation_trait.from_tbl {
TableRef::Table(tbl) => {
foreign_key_stmt.from_tbl(tbl);
}
_ => todo!(),
}
match relation_trait.to_tbl {
TableRef::Table(tbl) => {
foreign_key_stmt.to_tbl(tbl);
}
_ => todo!(),
}
match relation_trait.from_col {
crate::Identity::Unary(o1) => {
let from_tbl = unpack_table_ref(&relation.from_tbl);
let to_tbl = unpack_table_ref(&relation.to_tbl);
match relation.from_col {
Identity::Unary(o1) => {
foreign_key_stmt.from_col(o1);
}
crate::Identity::Binary(o1, o2) => {
Identity::Binary(o1, o2) => {
foreign_key_stmt.from_col(o1);
foreign_key_stmt.from_col(o2);
}
crate::Identity::Ternary(o1, o2, o3) => {
Identity::Ternary(o1, o2, o3) => {
foreign_key_stmt.from_col(o1);
foreign_key_stmt.from_col(o2);
foreign_key_stmt.from_col(o3);
}
}
match relation_trait.to_col {
crate::Identity::Unary(o1) => {
match relation.to_col {
Identity::Unary(o1) => {
foreign_key_stmt.to_col(o1);
}
crate::Identity::Binary(o1, o2) => {
Identity::Binary(o1, o2) => {
foreign_key_stmt.to_col(o1);
foreign_key_stmt.to_col(o2);
}
@ -58,200 +89,66 @@ pub trait CreateStatementOf {
foreign_key_stmt.to_col(o3);
}
}
stmt.foreign_key(&mut foreign_key_stmt);
stmt.foreign_key(
foreign_key_stmt
.name(&format!(
"fk-{}-{}",
from_tbl.to_string(),
to_tbl.to_string()
))
.from_tbl(from_tbl)
.to_tbl(to_tbl),
);
}
for col in E::Column::iter() {
let sea_orm_column_def: Sea_Orm_Column_Def = col.def().into();
let mut index = IndexCreateStatement::new();
let mut sea_query_column_def = Sea_Query_Column_Def::new(col);
for key in E::PrimaryKey::iter() {
// enum: Id, Name ...
if sea_query_column_def.get_column_name()
== Sea_Query_Column_Def::new(key.into_column()).get_column_name()
{
sea_query_column_def.primary_key();
if E::PrimaryKey::auto_increment() {
sea_query_column_def.auto_increment();
}
index.primary();
}
}
if !sea_orm_column_def.null {
sea_query_column_def.not_null();
}
if sea_orm_column_def.unique {
sea_query_column_def.unique_key();
index.unique();
}
if sea_orm_column_def.indexed {
index.table(entity);
index.col(col);
stmt.index(&mut index);
}
match Sea_Query_Column_Type::from(sea_orm_column_def.col_type) {
Sea_Query_Column_Type::Char(length) => match length {
Some(length) => {
sea_query_column_def.char_len(length);
}
None => {
sea_query_column_def.char();
}
},
Sea_Query_Column_Type::String(length) => match length {
Some(length) => {
sea_query_column_def.string_len(length);
}
None => {
sea_query_column_def.string();
}
},
Sea_Query_Column_Type::Text => {
sea_query_column_def.text();
}
Sea_Query_Column_Type::TinyInteger(length) => match length {
Some(length) => {
sea_query_column_def.tiny_integer_len(length);
}
None => {
sea_query_column_def.tiny_integer();
}
},
// Sea_Query_Column_Type::TinyInteger => { sea_query_column_def.tiny_integer(); },
Sea_Query_Column_Type::SmallInteger(length) => match length {
Some(length) => {
sea_query_column_def.small_integer_len(length);
}
None => {
sea_query_column_def.small_integer();
}
},
Sea_Query_Column_Type::Integer(length) => match length {
Some(length) => {
sea_query_column_def.integer_len(length);
}
None => {
sea_query_column_def.integer();
}
},
Sea_Query_Column_Type::BigInteger(length) => match length {
Some(length) => {
sea_query_column_def.big_integer_len(length);
}
None => {
sea_query_column_def.big_integer();
}
},
Sea_Query_Column_Type::Float(precision) => match precision {
Some(precision) => {
sea_query_column_def.float_len(precision);
}
None => {
sea_query_column_def.float();
}
},
Sea_Query_Column_Type::Double(precision) => match precision {
Some(precision) => {
sea_query_column_def.double_len(precision);
}
None => {
sea_query_column_def.double();
}
},
Sea_Query_Column_Type::Decimal(_) => {
sea_query_column_def.decimal();
}
Sea_Query_Column_Type::DateTime(precision) => match precision {
Some(precision) => {
sea_query_column_def.date_time_len(precision);
}
None => {
sea_query_column_def.date_time();
}
},
Sea_Query_Column_Type::Timestamp(precision) => match precision {
Some(precision) => {
sea_query_column_def.timestamp_len(precision);
}
None => {
sea_query_column_def.timestamp();
}
},
Sea_Query_Column_Type::Time(precision) => match precision {
Some(precision) => {
sea_query_column_def.time_len(precision);
}
None => {
sea_query_column_def.time();
}
},
Sea_Query_Column_Type::Date => {
sea_query_column_def.date();
}
Sea_Query_Column_Type::Binary(length) => match length {
Some(length) => {
sea_query_column_def.binary_len(length);
}
None => {
sea_query_column_def.binary();
}
},
Sea_Query_Column_Type::Boolean => {
sea_query_column_def.boolean();
}
Sea_Query_Column_Type::Money(_) => {
sea_query_column_def.money();
}
Sea_Query_Column_Type::Json => {
sea_query_column_def.json();
}
Sea_Query_Column_Type::JsonBinary => {
sea_query_column_def.json_binary();
}
Sea_Query_Column_Type::Custom(iden) => {
sea_query_column_def.custom(Alias::new(&iden.to_string()));
}
Sea_Query_Column_Type::Uuid => {
sea_query_column_def.uuid();
}
Sea_Query_Column_Type::TimestampWithTimeZone(length) => match length {
Some(length) => {
sea_query_column_def.timestamp_with_time_zone_len(length);
}
None => {
sea_query_column_def.timestamp_with_time_zone();
}
},
}
stmt.col(&mut sea_query_column_def);
}
stmt.if_not_exists();
stmt
}
stmt.table(entity).if_not_exists().take()
}
impl<EntityTrait> CreateStatementOf for EntityTrait {}
#[cfg(test)]
mod tests {
use crate::{tests_cfg, CreateStatementOf};
use crate::{entity_to_table_create_statement, tests_cfg::*};
use sea_query::*;
#[test]
fn test_create_statement_tests_cfg_cake() {
let create_statement =
tests_cfg::cake::Entity::create_table_statement_of(tests_cfg::cake::Entity);
let table = format!("{:?}", create_statement.get_table_name());
let columns = format!("{:?}", create_statement.get_columns());
let relations = format!("{:?}", create_statement.get_foreign_key_create_stmts());
let indexs = format!("{:?}", create_statement.get_indexes());
let result = format!("{:?}", create_statement);
assert_eq!("TableCreateStatement { table: Some(cake), columns: [ColumnDef { table: Some(cake), name: id, types: Some(Integer(None)), spec: [PrimaryKey, AutoIncrement, NotNull] }, ColumnDef { table: Some(cake), name: name, types: Some(String(None)), spec: [NotNull] }], options: [], partitions: [], indexes: [], foreign_keys: [ForeignKeyCreateStatement { foreign_key: TableForeignKey { name: None, table: Some(cake), ref_table: Some(fruit), columns: [id], ref_columns: [cake_id], on_delete: None, on_update: None } }], if_not_exists: true }", result);
assert_eq!(r#"Some("cake")"#, table);
assert_eq!("[ForeignKeyCreateStatement { foreign_key: TableForeignKey { name: None, table: Some(cake), ref_table: Some(fruit), columns: [id], ref_columns: [cake_id], on_delete: None, on_update: None } }]", relations);
fn test_entity_to_table_create_statement() {
assert_eq!(
r#"[ColumnDef { table: Some(cake), name: id, types: Some(Integer(None)), spec: [PrimaryKey, AutoIncrement, NotNull] }, ColumnDef { table: Some(cake), name: name, types: Some(String(None)), spec: [NotNull] }]"#,
columns
entity_to_table_create_statement(CakeFillingPrice).to_string(MysqlQueryBuilder),
Table::create()
.table(CakeFillingPrice)
.if_not_exists()
.col(
ColumnDef::new(cake_filling_price::Column::CakeId)
.integer()
.not_null()
)
.col(
ColumnDef::new(cake_filling_price::Column::FillingId)
.integer()
.not_null()
)
.col(
ColumnDef::new(cake_filling_price::Column::Price)
.decimal()
.not_null()
)
.primary_key(
Index::create()
.name("pk-cake_filling_price")
.col(cake_filling_price::Column::CakeId)
.col(cake_filling_price::Column::FillingId)
.primary()
)
.foreign_key(
ForeignKeyCreateStatement::new()
.name("fk-cake_filling_price-cake_filling")
.from_tbl(CakeFillingPrice)
.from_col(cake_filling_price::Column::CakeId)
.from_col(cake_filling_price::Column::FillingId)
.to_tbl(CakeFilling)
.to_col(cake_filling::Column::CakeId)
.to_col(cake_filling::Column::FillingId)
)
.to_string(MysqlQueryBuilder)
);
assert_eq!("[]", indexs);
}
}

View File

@ -295,7 +295,7 @@ fn join_condition(rel: RelationDef) -> SimpleExpr {
}
}
fn unpack_table_ref(table_ref: &TableRef) -> DynIden {
pub(crate) fn unpack_table_ref(table_ref: &TableRef) -> DynIden {
match table_ref {
TableRef::Table(tbl) => SeaRc::clone(tbl),
TableRef::SchemaTable(_, tbl) => SeaRc::clone(tbl),

View File

@ -49,7 +49,7 @@ impl ColumnTrait for Column {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::ContactDetails => ColumnType::Json.def(),
Self::BakeryId => ColumnType::Integer.def(),
Self::BakeryId => ColumnType::Integer.def().null(),
}
}
}

View File

@ -48,7 +48,7 @@ impl ColumnTrait for Column {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::ProfitMargin => ColumnType::Float.def(),
Self::ProfitMargin => ColumnType::Double.def(),
}
}
}

View File

@ -54,9 +54,9 @@ impl ColumnTrait for Column {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::Price => ColumnType::Decimal(Some((19, 4))).def(),
Self::BakeryId => ColumnType::Integer.def(),
Self::BakeryId => ColumnType::Integer.def().null(),
Self::GlutenFree => ColumnType::Boolean.def(),
Self::Serial => ColumnType::String(None).def(),
Self::Serial => ColumnType::Binary(Some(16)).def(),
}
}
}

View File

@ -46,7 +46,7 @@ impl ColumnTrait for Column {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::Notes => ColumnType::Text.def(),
Self::Notes => ColumnType::Text.def().null(),
}
}
}

View File

@ -1,15 +1,29 @@
use sea_orm::{error::*, sea_query, DbConn, ExecResult};
use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Index, TableCreateStatement};
use sea_orm::{
entity_to_table_create_statement, error::*, sea_query, DbConn, EntityTrait, ExecResult,
};
use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Index, Table, TableCreateStatement};
pub use super::super::bakery_chain::*;
async fn create_table(db: &DbConn, stmt: &TableCreateStatement) -> Result<ExecResult, DbErr> {
async fn create_table<E>(
db: &DbConn,
stmt: &TableCreateStatement,
entity: E,
) -> Result<ExecResult, DbErr>
where
E: EntityTrait,
{
let builder = db.get_database_backend();
db.execute(builder.build(stmt)).await
let stmt = builder.build(stmt);
assert_eq!(
builder.build(&entity_to_table_create_statement(entity)),
stmt
);
db.execute(stmt).await
}
pub async fn create_bakery_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
let stmt = Table::create()
.table(bakery::Entity)
.if_not_exists()
.col(
@ -27,16 +41,17 @@ pub async fn create_bakery_table(db: &DbConn) -> Result<ExecResult, DbErr> {
)
.to_owned();
create_table(db, &stmt).await
create_table(db, &stmt, Bakery).await
}
pub async fn create_baker_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
let stmt = Table::create()
.table(baker::Entity)
.if_not_exists()
.col(
ColumnDef::new(baker::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
@ -49,19 +64,19 @@ pub async fn create_baker_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.col(ColumnDef::new(baker::Column::BakeryId).integer())
.foreign_key(
ForeignKey::create()
.name("FK_baker_bakery")
.name("fk-baker-bakery")
.from(baker::Entity, baker::Column::BakeryId)
.to(bakery::Entity, bakery::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
)
.to_owned();
create_table(db, &stmt).await
create_table(db, &stmt, Baker).await
}
pub async fn create_customer_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
let stmt = Table::create()
.table(customer::Entity)
.if_not_exists()
.col(
@ -75,11 +90,11 @@ pub async fn create_customer_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.col(ColumnDef::new(customer::Column::Notes).text())
.to_owned();
create_table(db, &stmt).await
create_table(db, &stmt, Customer).await
}
pub async fn create_order_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
let stmt = Table::create()
.table(order::Entity)
.if_not_exists()
.col(
@ -107,27 +122,27 @@ pub async fn create_order_table(db: &DbConn) -> Result<ExecResult, DbErr> {
)
.foreign_key(
ForeignKey::create()
.name("FK_order_bakery")
.name("fk-order-bakery")
.from(order::Entity, order::Column::BakeryId)
.to(bakery::Entity, bakery::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("FK_order_customer")
.name("fk-order-customer")
.from(order::Entity, order::Column::CustomerId)
.to(customer::Entity, customer::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
)
.to_owned();
create_table(db, &stmt).await
create_table(db, &stmt, Order).await
}
pub async fn create_lineitem_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
let stmt = Table::create()
.table(lineitem::Entity)
.if_not_exists()
.col(
@ -159,27 +174,27 @@ pub async fn create_lineitem_table(db: &DbConn) -> Result<ExecResult, DbErr> {
)
.foreign_key(
ForeignKey::create()
.name("FK_lineitem_order")
.name("fk-lineitem-order")
.from(lineitem::Entity, lineitem::Column::OrderId)
.to(order::Entity, order::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("FK_lineitem_cake")
.name("fk-lineitem-cake")
.from(lineitem::Entity, lineitem::Column::CakeId)
.to(cake::Entity, cake::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
)
.to_owned();
create_table(db, &stmt).await
create_table(db, &stmt, Lineitem).await
}
pub async fn create_cakes_bakers_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
let stmt = Table::create()
.table(cakes_bakers::Entity)
.if_not_exists()
.col(
@ -194,32 +209,33 @@ pub async fn create_cakes_bakers_table(db: &DbConn) -> Result<ExecResult, DbErr>
)
.primary_key(
Index::create()
.name("pk-cakes_bakers")
.col(cakes_bakers::Column::CakeId)
.col(cakes_bakers::Column::BakerId),
)
.foreign_key(
ForeignKey::create()
.name("FK_cakes_bakers_cake")
.name("fk-cakes_bakers-cake")
.from(cakes_bakers::Entity, cakes_bakers::Column::CakeId)
.to(cake::Entity, cake::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("FK_cakes_bakers_baker")
.name("fk-cakes_bakers-baker")
.from(cakes_bakers::Entity, cakes_bakers::Column::BakerId)
.to(baker::Entity, baker::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
)
.to_owned();
create_table(db, &stmt).await
create_table(db, &stmt, CakesBakers).await
}
pub async fn create_cake_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
let stmt = Table::create()
.table(cake::Entity)
.if_not_exists()
.col(
@ -238,11 +254,11 @@ pub async fn create_cake_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.col(ColumnDef::new(cake::Column::BakeryId).integer())
.foreign_key(
ForeignKey::create()
.name("FK_cake_bakery")
.name("fk-cake-bakery")
.from(cake::Entity, cake::Column::BakeryId)
.to(bakery::Entity, bakery::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
)
.col(
ColumnDef::new(cake::Column::GlutenFree)
@ -252,5 +268,5 @@ pub async fn create_cake_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.col(ColumnDef::new(cake::Column::Serial).uuid().not_null())
.to_owned();
create_table(db, &stmt).await
create_table(db, &stmt, Cake).await
}