-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
1 deletion.
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,22 @@ | ||
[workspace] | ||
# A separate workspace | ||
|
||
[package] | ||
name = "sea-orm-issues-693" | ||
version = "0.1.0" | ||
authors = ["Sebastian Pütz <[email protected]>"] | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
tokio = { version = "1.14", features = ["full"] } | ||
anyhow = "1" | ||
dotenv = "0.15" | ||
futures-util = "0.3" | ||
serde = "1" | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
|
||
[dependencies.sea-orm] | ||
path = "../../" # remove this line in your own project | ||
features = ["runtime-tokio-rustls", "sqlx-mysql", "macros"] | ||
default-features = false |
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,31 @@ | ||
pub mod prelude { | ||
pub use super::model::{ | ||
ActiveModel as ContainerActiveModel, Column as ContainerColumn, Entity as Container, | ||
Model as ContainerModel, PrimaryKey as ContainerPrimaryKey, Relation as ContainerRelation, | ||
}; | ||
} | ||
|
||
pub mod model { | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "container")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, column_name = "db_id")] | ||
pub rust_id: i32, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm(has_many = "crate::Content")] | ||
Content, // 1(Container) ⇆ n(Content) | ||
} | ||
|
||
impl Related<crate::Content> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Content.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,36 @@ | ||
pub mod prelude { | ||
pub use super::model::{ | ||
ActiveModel as ContentActiveModel, Column as ContentColumn, Entity as Content, | ||
Model as ContentModel, PrimaryKey as ContentPrimaryKey, Relation as ContentRelation, | ||
}; | ||
} | ||
|
||
pub mod model { | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "content")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
pub container_id: i32, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation { | ||
#[sea_orm( | ||
belongs_to = "crate::Container", | ||
from = "crate::ContentColumn::ContainerId", | ||
to = "crate::ContainerColumn::RustId" | ||
)] | ||
Container, // 1(Container) ⇆ n(Content) | ||
} | ||
|
||
impl Related<crate::Container> for Entity { | ||
fn to() -> RelationDef { | ||
Relation::Container.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,18 @@ | ||
mod container; | ||
mod content; | ||
|
||
use container::prelude::*; | ||
use content::prelude::*; | ||
use sea_orm::{DbBackend, EntityTrait, QueryTrait}; | ||
|
||
fn main() { | ||
assert_eq!( | ||
Container::find().find_with_related(Content).build(DbBackend::MySql).to_string(), | ||
[ | ||
"SELECT `container`.`db_id` AS `A_db_id`, `content`.`id` AS `B_id`, `content`.`container_id` AS `B_container_id`", | ||
"FROM `container`", | ||
"LEFT JOIN `content` ON `container`.`db_id` = `content`.`container_id`", | ||
"ORDER BY `container`.`db_id` ASC", | ||
].join(" ") | ||
); | ||
} |