Skip to content

Commit 0833478

Browse files
committed
derive seaography::RelationBuilder only when seaography feature is enabled
1 parent 433f7c4 commit 0833478

File tree

10 files changed

+347
-3
lines changed

10 files changed

+347
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,4 @@ runtime-tokio-rustls = [
121121
"runtime-tokio",
122122
]
123123
tests-cfg = ["serde/derive"]
124+
seaography = ["sea-orm-macros/seaography"]

issues/1599/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[workspace]
2+
# A separate workspace
3+
4+
[package]
5+
name = "sea-orm-issues-1599"
6+
version = "0.1.0"
7+
edition = "2021"
8+
publish = false
9+
10+
[dependencies]
11+
anyhow = "1"
12+
serde = "1"
13+
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
14+
seaography = { path = "../../../seaography", optional = true }
15+
async-graphql = { version = "5", optional = true }
16+
17+
[dependencies.sea-orm]
18+
path = "../../"
19+
default-features = false
20+
features = ["macros"]
21+
22+
[features]
23+
default = ["seaography"]
24+
seaography = ["dep:seaography", "async-graphql", "sea-orm/seaography"]

issues/1599/src/cake.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use sea_orm::entity::prelude::*;
2+
3+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
4+
#[sea_orm(table_name = "cake")]
5+
pub struct Model {
6+
#[sea_orm(primary_key)]
7+
pub id: i32,
8+
#[sea_orm(column_name = "name", enum_name = "Name")]
9+
pub name: String,
10+
}
11+
12+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
13+
pub enum Relation {
14+
#[sea_orm(has_many = "super::fruit::Entity")]
15+
Fruit,
16+
#[sea_orm(
17+
has_many = "super::fruit::Entity",
18+
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"#
19+
)]
20+
TropicalFruit,
21+
#[sea_orm(
22+
has_many = "super::fruit::Entity",
23+
condition_type = "any",
24+
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"#
25+
)]
26+
OrTropicalFruit,
27+
}
28+
29+
impl Related<super::fruit::Entity> for Entity {
30+
fn to() -> RelationDef {
31+
Relation::Fruit.def()
32+
}
33+
}
34+
35+
impl Related<super::filling::Entity> for Entity {
36+
fn to() -> RelationDef {
37+
super::cake_filling::Relation::Filling.def()
38+
}
39+
40+
fn via() -> Option<RelationDef> {
41+
Some(super::cake_filling::Relation::Cake.def().rev())
42+
}
43+
}
44+
45+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)]
46+
pub enum RelatedEntity {
47+
#[sea_orm(entity = "super::fruit::Entity")]
48+
Fruit,
49+
#[sea_orm(entity = "super::filling::Entity")]
50+
Filling,
51+
#[sea_orm(entity = "super::fruit::Entity", def = "Relation::TropicalFruit.def()")]
52+
TropicalFruit,
53+
#[sea_orm(
54+
entity = "super::fruit::Entity",
55+
def = "Relation::OrTropicalFruit.def()"
56+
)]
57+
OrTropicalFruit,
58+
}
59+
60+
impl ActiveModelBehavior for ActiveModel {}

issues/1599/src/cake_filling.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use sea_orm::entity::prelude::*;
2+
3+
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
4+
pub struct Entity;
5+
6+
impl EntityName for Entity {
7+
fn table_name(&self) -> &str {
8+
"cake_filling"
9+
}
10+
}
11+
12+
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
13+
pub struct Model {
14+
pub cake_id: i32,
15+
pub filling_id: i32,
16+
}
17+
18+
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
19+
pub enum Column {
20+
CakeId,
21+
FillingId,
22+
}
23+
24+
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
25+
pub enum PrimaryKey {
26+
CakeId,
27+
FillingId,
28+
}
29+
30+
impl PrimaryKeyTrait for PrimaryKey {
31+
type ValueType = (i32, i32);
32+
33+
fn auto_increment() -> bool {
34+
false
35+
}
36+
}
37+
38+
#[derive(Copy, Clone, Debug, EnumIter)]
39+
pub enum Relation {
40+
Cake,
41+
Filling,
42+
}
43+
44+
impl ColumnTrait for Column {
45+
type EntityName = Entity;
46+
47+
fn def(&self) -> ColumnDef {
48+
match self {
49+
Self::CakeId => ColumnType::Integer.def(),
50+
Self::FillingId => ColumnType::Integer.def(),
51+
}
52+
}
53+
}
54+
55+
impl RelationTrait for Relation {
56+
fn def(&self) -> RelationDef {
57+
match self {
58+
Self::Cake => Entity::belongs_to(super::cake::Entity)
59+
.from(Column::CakeId)
60+
.to(super::cake::Column::Id)
61+
.into(),
62+
Self::Filling => Entity::belongs_to(super::filling::Entity)
63+
.from(Column::FillingId)
64+
.to(super::filling::Column::Id)
65+
.into(),
66+
}
67+
}
68+
}
69+
70+
impl ActiveModelBehavior for ActiveModel {}

issues/1599/src/filling.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use sea_orm::entity::prelude::*;
2+
3+
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
4+
#[sea_orm(table_name = "filling")]
5+
pub struct Entity;
6+
7+
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
8+
pub struct Model {
9+
pub id: i32,
10+
pub name: String,
11+
pub vendor_id: Option<i32>,
12+
#[sea_orm(ignore)]
13+
pub ignored_attr: i32,
14+
}
15+
16+
// If your column names are not in snake-case, derive `DeriveCustomColumn` here.
17+
#[derive(Copy, Clone, Debug, EnumIter, DeriveCustomColumn)]
18+
pub enum Column {
19+
Id,
20+
Name,
21+
VendorId,
22+
}
23+
24+
// Then, customize each column names here.
25+
impl IdenStatic for Column {
26+
fn as_str(&self) -> &str {
27+
match self {
28+
// Override column names
29+
Self::Id => "id",
30+
// Leave all other columns using default snake-case values
31+
_ => self.default_as_str(),
32+
}
33+
}
34+
}
35+
36+
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
37+
pub enum PrimaryKey {
38+
Id,
39+
}
40+
41+
impl PrimaryKeyTrait for PrimaryKey {
42+
type ValueType = i32;
43+
44+
fn auto_increment() -> bool {
45+
true
46+
}
47+
}
48+
49+
#[derive(Copy, Clone, Debug, EnumIter)]
50+
pub enum Relation {}
51+
52+
impl ColumnTrait for Column {
53+
type EntityName = Entity;
54+
55+
fn def(&self) -> ColumnDef {
56+
match self {
57+
Self::Id => ColumnType::Integer.def(),
58+
Self::Name => ColumnType::String(None).def(),
59+
Self::VendorId => ColumnType::Integer.def().nullable(),
60+
}
61+
}
62+
}
63+
64+
impl RelationTrait for Relation {
65+
fn def(&self) -> RelationDef {
66+
panic!("No RelationDef")
67+
}
68+
}
69+
70+
impl Related<super::cake::Entity> for Entity {
71+
fn to() -> RelationDef {
72+
super::cake_filling::Relation::Cake.def()
73+
}
74+
75+
fn via() -> Option<RelationDef> {
76+
Some(super::cake_filling::Relation::Filling.def().rev())
77+
}
78+
}
79+
80+
impl ActiveModelBehavior for ActiveModel {}

issues/1599/src/fruit.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use sea_orm::entity::prelude::*;
2+
3+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
4+
#[sea_orm(table_name = "fruit")]
5+
pub struct Model {
6+
#[sea_orm(primary_key)]
7+
#[cfg_attr(feature = "with-json", serde(skip_deserializing))]
8+
pub id: i32,
9+
pub name: String,
10+
pub cake_id: Option<i32>,
11+
}
12+
13+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
14+
pub enum Relation {
15+
#[sea_orm(
16+
belongs_to = "super::cake::Entity",
17+
from = "Column::CakeId",
18+
to = "super::cake::Column::Id"
19+
)]
20+
Cake,
21+
}
22+
23+
impl Related<super::cake::Entity> for Entity {
24+
fn to() -> RelationDef {
25+
Relation::Cake.def()
26+
}
27+
}
28+
29+
impl ActiveModelBehavior for ActiveModel {}

issues/1599/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use anyhow::Result;
2+
3+
pub mod cake;
4+
pub mod cake_filling;
5+
pub mod filling;
6+
pub mod fruit;
7+
8+
#[tokio::main]
9+
async fn main() -> Result<()> {
10+
Ok(())
11+
}

sea-orm-macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ default = ["derive"]
3434
postgres-array = []
3535
derive = ["bae"]
3636
strum = []
37+
seaography = []

sea-orm-macros/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,13 @@ pub fn derive_relation(input: TokenStream) -> TokenStream {
670670
#[proc_macro_derive(DeriveRelatedEntity, attributes(sea_orm))]
671671
pub fn derive_related_entity(input: TokenStream) -> TokenStream {
672672
let input = parse_macro_input!(input as DeriveInput);
673-
derives::expand_derive_related_entity(input)
674-
.unwrap_or_else(Error::into_compile_error)
675-
.into()
673+
if cfg!(feature = "seaography") {
674+
derives::expand_derive_related_entity(input)
675+
.unwrap_or_else(Error::into_compile_error)
676+
.into()
677+
} else {
678+
TokenStream::new()
679+
}
676680
}
677681

678682
/// The DeriveMigrationName derive macro will implement `sea_orm_migration::MigrationName` for a migration.

src/tests_cfg/cake_seaography.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use crate as sea_orm;
2+
use crate::entity::prelude::*;
3+
4+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
5+
#[sea_orm(table_name = "cake")]
6+
pub struct Model {
7+
#[sea_orm(primary_key)]
8+
pub id: i32,
9+
#[sea_orm(column_name = "name", enum_name = "Name")]
10+
pub name: String,
11+
}
12+
13+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
14+
pub enum Relation {
15+
#[sea_orm(has_many = "super::fruit::Entity")]
16+
Fruit,
17+
#[sea_orm(
18+
has_many = "super::fruit::Entity",
19+
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"#
20+
)]
21+
TropicalFruit,
22+
#[sea_orm(
23+
has_many = "super::fruit::Entity",
24+
condition_type = "any",
25+
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"#
26+
)]
27+
OrTropicalFruit,
28+
}
29+
30+
impl Related<super::fruit::Entity> for Entity {
31+
fn to() -> RelationDef {
32+
Relation::Fruit.def()
33+
}
34+
}
35+
36+
impl Related<super::filling::Entity> for Entity {
37+
fn to() -> RelationDef {
38+
super::cake_filling::Relation::Filling.def()
39+
}
40+
41+
fn via() -> Option<RelationDef> {
42+
Some(super::cake_filling::Relation::Cake.def().rev())
43+
}
44+
}
45+
46+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)]
47+
pub enum RelatedEntity {
48+
#[sea_orm(entity = "super::fruit::Entity")]
49+
Fruit,
50+
#[sea_orm(entity = "super::filling::Entity")]
51+
Filling,
52+
#[sea_orm(
53+
entity = "super::fruit::Entity",
54+
def = "Relation::TropicalFruit.def()"
55+
)]
56+
TropicalFruit,
57+
#[sea_orm(
58+
entity = "super::fruit::Entity",
59+
def = "Relation::OrTropicalFruit.def()"
60+
)]
61+
OrTropicalFruit,
62+
}
63+
64+
impl ActiveModelBehavior for ActiveModel {}

0 commit comments

Comments
 (0)