Baker belongs to a bakery

This commit is contained in:
Sam Samai 2021-06-26 17:58:37 +10:00
parent 318665d1af
commit bd2e7a922f
4 changed files with 168 additions and 33 deletions

View File

@ -0,0 +1,71 @@
use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"baker"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
pub id: i32,
pub name: String,
pub bakery_id: Option<i32>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Name,
BakeryId,
}
#[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,
}
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::BakeryId => ColumnType::Integer.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(),
}
}
}
impl Related<super::bakery::Entity> for Entity {
fn to() -> RelationDef {
Relation::Bakery.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -0,0 +1,74 @@
use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"bakery"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
pub id: i32,
pub name: String,
pub profit_margin: f64,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Name,
ProfitMargin,
}
#[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 {
Baker,
}
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::ProfitMargin => ColumnType::Float.def(),
}
}
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Baker => Entity::has_many(super::baker::Entity).into(),
}
}
}
impl Related<super::baker::Entity> for Entity {
fn to() -> RelationDef {
Relation::Baker.def()
}
}
impl Model {
pub fn find_bakers(&self) -> Select<super::baker::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -0,0 +1,2 @@
pub mod baker;
pub mod bakery;

View File

@ -1,26 +1,12 @@
use sea_orm::{sea_query, DbConn, ExecErr, ExecResult};
use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, Iden, SqliteQueryBuilder};
use sea_query::{ColumnDef, ForeignKey, ForeignKeyAction, SqliteQueryBuilder};
pub mod bakery_chain;
mod setup;
#[derive(Iden)]
enum Bakery {
Table,
Id,
Name,
ProfitMargin,
}
#[derive(Iden)]
enum Baker {
Table,
Id,
Name,
BakeryId,
}
pub use bakery_chain::*;
#[async_std::test]
// cargo test --test bakery -- --nocapture
// cargo test --test bakery_chain_tests -- --nocapture
async fn main() {
let db: DbConn = setup::setup().await;
setup_schema(&db).await;
@ -33,41 +19,43 @@ async fn setup_schema(db: &DbConn) {
async fn create_bakery(db: &DbConn) -> Result<ExecResult, ExecErr> {
let stmt = sea_query::Table::create()
.table(Bakery::Table)
.table(bakery::Entity)
.if_not_exists()
.col(
ColumnDef::new(Bakery::Id)
ColumnDef::new(bakery::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Bakery::Name).string())
.col(ColumnDef::new(Bakery::ProfitMargin).float())
.col(ColumnDef::new(bakery::Column::Name).string())
.col(ColumnDef::new(bakery::Column::ProfitMargin).float())
.build(SqliteQueryBuilder);
db.execute(stmt.into()).await
}
async fn create_baker(db: &DbConn) -> Result<ExecResult, ExecErr> {
let stmt = sea_query::Table::create()
.table(Baker::Table)
.table(baker::Entity)
.if_not_exists()
.col(
ColumnDef::new(Baker::Id)
ColumnDef::new(baker::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Baker::Name).string())
// .foreign_key(
// ForeignKey::create()
// .name("FK_baker_bakery")
// .from(Baker::Table, Baker::BakeryId)
// .to(Bakery::Table, Bakery::Id)
// .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade),
// )
.col(ColumnDef::new(baker::Column::Name).string())
.col(ColumnDef::new(baker::Column::BakeryId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_baker_bakery")
.from(baker::Entity, baker::Column::BakeryId)
.to(bakery::Entity, bakery::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.build(SqliteQueryBuilder);
db.execute(stmt.clone().into()).await