Slide 19
Slide 19 text
モデル定義
: order モデルの定義
// src/order.rs
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, DeriveEntityModel)]
#[sea_orm(table_name = "orders")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = true)]
pub order_id: i32,
pub ordered_at: DateTime,
pub customer_id: i32,
pub item_id: i32,
pub amount: u32,
}
#[derive(Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::customer::Entity",
from = "Column::CustomerId",
to = "super::customer::Column::CustomerId"
)]
Customer,
#[sea_orm(
belongs_to = "super::item::Entity",
from = "Column::ItemId",
to = "super::item::Column::ItemId"
)]
Item,
}
impl Related for Entity {
fn to() -> RelationDef {
Relation::Customer.def()
}
}
impl Related for Entity {
fn to() -> RelationDef {
Relation::Item.def()
}
}
impl ActiveModelBehavior for ActiveModel {}