-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
derive
seaography::RelationBuilder
only when seaography
feature i…
…s enabled
- Loading branch information
Showing
10 changed files
with
347 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[workspace] | ||
# A separate workspace | ||
|
||
[package] | ||
name = "sea-orm-issues-1599" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
serde = "1" | ||
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] } | ||
seaography = { path = "../../../seaography", optional = true } | ||
async-graphql = { version = "5", optional = true } | ||
|
||
[dependencies.sea-orm] | ||
path = "../../" | ||
default-features = false | ||
features = ["macros"] | ||
|
||
[features] | ||
default = ["seaography"] | ||
seaography = ["dep:seaography", "async-graphql", "sea-orm/seaography"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "cake")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
#[sea_orm(column_name = "name", enum_name = "Name")] | ||
pub name: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm(has_many = "super::fruit::Entity")] | ||
Fruit, | ||
#[sea_orm( | ||
has_many = "super::fruit::Entity", | ||
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"# | ||
)] | ||
TropicalFruit, | ||
#[sea_orm( | ||
has_many = "super::fruit::Entity", | ||
condition_type = "any", | ||
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"# | ||
)] | ||
OrTropicalFruit, | ||
} | ||
|
||
impl Related<super::fruit::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Fruit.def() | ||
} | ||
} | ||
|
||
impl Related<super::filling::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
super::cake_filling::Relation::Filling.def() | ||
} | ||
|
||
fn via() -> Option<RelationDef> { | ||
Some(super::cake_filling::Relation::Cake.def().rev()) | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)] | ||
pub enum RelatedEntity { | ||
#[sea_orm(entity = "super::fruit::Entity")] | ||
Fruit, | ||
#[sea_orm(entity = "super::filling::Entity")] | ||
Filling, | ||
#[sea_orm(entity = "super::fruit::Entity", def = "Relation::TropicalFruit.def()")] | ||
TropicalFruit, | ||
#[sea_orm( | ||
entity = "super::fruit::Entity", | ||
def = "Relation::OrTropicalFruit.def()" | ||
)] | ||
OrTropicalFruit, | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] | ||
pub struct Entity; | ||
|
||
impl EntityName for Entity { | ||
fn table_name(&self) -> &str { | ||
"cake_filling" | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] | ||
pub struct Model { | ||
pub cake_id: i32, | ||
pub filling_id: i32, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] | ||
pub enum Column { | ||
CakeId, | ||
FillingId, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] | ||
pub enum PrimaryKey { | ||
CakeId, | ||
FillingId, | ||
} | ||
|
||
impl PrimaryKeyTrait for PrimaryKey { | ||
type ValueType = (i32, i32); | ||
|
||
fn auto_increment() -> bool { | ||
false | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter)] | ||
pub enum Relation { | ||
Cake, | ||
Filling, | ||
} | ||
|
||
impl ColumnTrait for Column { | ||
type EntityName = Entity; | ||
|
||
fn def(&self) -> ColumnDef { | ||
match self { | ||
Self::CakeId => ColumnType::Integer.def(), | ||
Self::FillingId => 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::Filling => Entity::belongs_to(super::filling::Entity) | ||
.from(Column::FillingId) | ||
.to(super::filling::Column::Id) | ||
.into(), | ||
} | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] | ||
#[sea_orm(table_name = "filling")] | ||
pub struct Entity; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] | ||
pub struct Model { | ||
pub id: i32, | ||
pub name: String, | ||
pub vendor_id: Option<i32>, | ||
#[sea_orm(ignore)] | ||
pub ignored_attr: i32, | ||
} | ||
|
||
// If your column names are not in snake-case, derive `DeriveCustomColumn` here. | ||
#[derive(Copy, Clone, Debug, EnumIter, DeriveCustomColumn)] | ||
pub enum Column { | ||
Id, | ||
Name, | ||
VendorId, | ||
} | ||
|
||
// Then, customize each column names here. | ||
impl IdenStatic for Column { | ||
fn as_str(&self) -> &str { | ||
match self { | ||
// Override column names | ||
Self::Id => "id", | ||
// Leave all other columns using default snake-case values | ||
_ => self.default_as_str(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] | ||
pub enum PrimaryKey { | ||
Id, | ||
} | ||
|
||
impl PrimaryKeyTrait for PrimaryKey { | ||
type ValueType = i32; | ||
|
||
fn auto_increment() -> bool { | ||
true | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter)] | ||
pub enum Relation {} | ||
|
||
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::VendorId => ColumnType::Integer.def().nullable(), | ||
} | ||
} | ||
} | ||
|
||
impl RelationTrait for Relation { | ||
fn def(&self) -> RelationDef { | ||
panic!("No RelationDef") | ||
} | ||
} | ||
|
||
impl Related<super::cake::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
super::cake_filling::Relation::Cake.def() | ||
} | ||
|
||
fn via() -> Option<RelationDef> { | ||
Some(super::cake_filling::Relation::Filling.def().rev()) | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "fruit")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
#[cfg_attr(feature = "with-json", serde(skip_deserializing))] | ||
pub id: i32, | ||
pub name: String, | ||
pub cake_id: Option<i32>, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm( | ||
belongs_to = "super::cake::Entity", | ||
from = "Column::CakeId", | ||
to = "super::cake::Column::Id" | ||
)] | ||
Cake, | ||
} | ||
|
||
impl Related<super::cake::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Cake.def() | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use anyhow::Result; | ||
|
||
pub mod cake; | ||
pub mod cake_filling; | ||
pub mod filling; | ||
pub mod fruit; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ default = ["derive"] | |
postgres-array = [] | ||
derive = ["bae"] | ||
strum = [] | ||
seaography = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate as sea_orm; | ||
use crate::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "cake")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
#[sea_orm(column_name = "name", enum_name = "Name")] | ||
pub name: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm(has_many = "super::fruit::Entity")] | ||
Fruit, | ||
#[sea_orm( | ||
has_many = "super::fruit::Entity", | ||
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"# | ||
)] | ||
TropicalFruit, | ||
#[sea_orm( | ||
has_many = "super::fruit::Entity", | ||
condition_type = "any", | ||
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"# | ||
)] | ||
OrTropicalFruit, | ||
} | ||
|
||
impl Related<super::fruit::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Fruit.def() | ||
} | ||
} | ||
|
||
impl Related<super::filling::Entity> for Entity { | ||
fn to() -> RelationDef { | ||
super::cake_filling::Relation::Filling.def() | ||
} | ||
|
||
fn via() -> Option<RelationDef> { | ||
Some(super::cake_filling::Relation::Cake.def().rev()) | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)] | ||
pub enum RelatedEntity { | ||
#[sea_orm(entity = "super::fruit::Entity")] | ||
Fruit, | ||
#[sea_orm(entity = "super::filling::Entity")] | ||
Filling, | ||
#[sea_orm( | ||
entity = "super::fruit::Entity", | ||
def = "Relation::TropicalFruit.def()" | ||
)] | ||
TropicalFruit, | ||
#[sea_orm( | ||
entity = "super::fruit::Entity", | ||
def = "Relation::OrTropicalFruit.def()" | ||
)] | ||
OrTropicalFruit, | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |