Skip to content

Commit

Permalink
codegen: testing entity generation of composite foreign key (#2071)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored Jan 19, 2024
1 parent f2bda8a commit c6e1a84
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 4 deletions.
96 changes: 92 additions & 4 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,86 @@ mod tests {
name: "id".to_owned(),
}],
},
Entity {
table_name: "parent".to_owned(),
columns: vec![
Column {
name: "id1".to_owned(),
col_type: ColumnType::Integer,
auto_increment: false,
not_null: true,
unique: false,
},
Column {
name: "id2".to_owned(),
col_type: ColumnType::Integer,
auto_increment: false,
not_null: true,
unique: false,
},
],
relations: vec![Relation {
ref_table: "child".to_owned(),
columns: vec![],
ref_columns: vec![],
rel_type: RelationType::HasMany,
on_delete: None,
on_update: None,
self_referencing: false,
num_suffix: 0,
impl_related: true,
}],
conjunct_relations: vec![],
primary_keys: vec![
PrimaryKey {
name: "id1".to_owned(),
},
PrimaryKey {
name: "id2".to_owned(),
},
],
},
Entity {
table_name: "child".to_owned(),
columns: vec![
Column {
name: "id".to_owned(),
col_type: ColumnType::Integer,
auto_increment: true,
not_null: true,
unique: false,
},
Column {
name: "parent_id1".to_owned(),
col_type: ColumnType::Integer,
auto_increment: false,
not_null: true,
unique: false,
},
Column {
name: "parent_id2".to_owned(),
col_type: ColumnType::Integer,
auto_increment: false,
not_null: true,
unique: false,
},
],
relations: vec![Relation {
ref_table: "parent".to_owned(),
columns: vec!["parent_id1".to_owned(), "parent_id2".to_owned()],
ref_columns: vec!["id1".to_owned(), "id2".to_owned()],
rel_type: RelationType::BelongsTo,
on_delete: None,
on_update: None,
self_referencing: false,
num_suffix: 0,
impl_related: true,
}],
conjunct_relations: vec![],
primary_keys: vec![PrimaryKey {
name: "id".to_owned(),
}],
},
]
}

Expand All @@ -1434,7 +1514,7 @@ mod tests {
#[test]
fn test_gen_expanded_code_blocks() -> io::Result<()> {
let entities = setup();
const ENTITY_FILES: [&str; 11] = [
const ENTITY_FILES: [&str; 13] = [
include_str!("../../tests/expanded/cake.rs"),
include_str!("../../tests/expanded/cake_filling.rs"),
include_str!("../../tests/expanded/cake_filling_price.rs"),
Expand All @@ -1446,8 +1526,10 @@ mod tests {
include_str!("../../tests/expanded/cake_with_double.rs"),
include_str!("../../tests/expanded/collection.rs"),
include_str!("../../tests/expanded/collection_float.rs"),
include_str!("../../tests/expanded/parent.rs"),
include_str!("../../tests/expanded/child.rs"),
];
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 11] = [
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [
include_str!("../../tests/expanded_with_schema_name/cake.rs"),
include_str!("../../tests/expanded_with_schema_name/cake_filling.rs"),
include_str!("../../tests/expanded_with_schema_name/cake_filling_price.rs"),
Expand All @@ -1459,6 +1541,8 @@ mod tests {
include_str!("../../tests/expanded_with_schema_name/cake_with_double.rs"),
include_str!("../../tests/expanded_with_schema_name/collection.rs"),
include_str!("../../tests/expanded_with_schema_name/collection_float.rs"),
include_str!("../../tests/expanded_with_schema_name/parent.rs"),
include_str!("../../tests/expanded_with_schema_name/child.rs"),
];

assert_eq!(entities.len(), ENTITY_FILES.len());
Expand Down Expand Up @@ -1535,7 +1619,7 @@ mod tests {
#[test]
fn test_gen_compact_code_blocks() -> io::Result<()> {
let entities = setup();
const ENTITY_FILES: [&str; 11] = [
const ENTITY_FILES: [&str; 13] = [
include_str!("../../tests/compact/cake.rs"),
include_str!("../../tests/compact/cake_filling.rs"),
include_str!("../../tests/compact/cake_filling_price.rs"),
Expand All @@ -1547,8 +1631,10 @@ mod tests {
include_str!("../../tests/compact/cake_with_double.rs"),
include_str!("../../tests/compact/collection.rs"),
include_str!("../../tests/compact/collection_float.rs"),
include_str!("../../tests/compact/parent.rs"),
include_str!("../../tests/compact/child.rs"),
];
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 11] = [
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [
include_str!("../../tests/compact_with_schema_name/cake.rs"),
include_str!("../../tests/compact_with_schema_name/cake_filling.rs"),
include_str!("../../tests/compact_with_schema_name/cake_filling_price.rs"),
Expand All @@ -1560,6 +1646,8 @@ mod tests {
include_str!("../../tests/compact_with_schema_name/cake_with_double.rs"),
include_str!("../../tests/compact_with_schema_name/collection.rs"),
include_str!("../../tests/compact_with_schema_name/collection_float.rs"),
include_str!("../../tests/compact_with_schema_name/parent.rs"),
include_str!("../../tests/compact_with_schema_name/child.rs"),
];

assert_eq!(entities.len(), ENTITY_FILES.len());
Expand Down
30 changes: 30 additions & 0 deletions sea-orm-codegen/tests/compact/child.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "child")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub parent_id1: i32,
pub parent_id2: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::parent::Entity",
from = "(Column::ParentId1, Column::ParentId2)",
to = "(super::parent::Column::Id1, super::parent::Column::Id2)",
)]
Parent,
}

impl Related<super::parent::Entity> for Entity {
fn to() -> RelationDef {
Relation::Parent.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
26 changes: 26 additions & 0 deletions sea-orm-codegen/tests/compact/parent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "parent")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id2: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::child::Entity")]
Child,
}

impl Related<super::child::Entity> for Entity {
fn to() -> RelationDef {
Relation::Child.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
30 changes: 30 additions & 0 deletions sea-orm-codegen/tests/compact_with_schema_name/child.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(schema_name = "schema_name", table_name = "child")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub parent_id1: i32,
pub parent_id2: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::parent::Entity",
from = "(Column::ParentId1, Column::ParentId2)",
to = "(super::parent::Column::Id1, super::parent::Column::Id2)",
)]
Parent,
}

impl Related<super::parent::Entity> for Entity {
fn to() -> RelationDef {
Relation::Parent.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
26 changes: 26 additions & 0 deletions sea-orm-codegen/tests/compact_with_schema_name/parent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(schema_name = "schema_name", table_name = "parent")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id2: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::child::Entity")]
Child,
}

impl Related<super::child::Entity> for Entity {
fn to() -> RelationDef {
Relation::Child.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
73 changes: 73 additions & 0 deletions sea-orm-codegen/tests/expanded/child.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0
use sea_orm::entity::prelude::*;

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;

impl EntityName for Entity {
fn table_name(&self) -> &str {
"child"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
pub struct Model {
pub id: i32,
pub parent_id1: i32,
pub parent_id2: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
ParentId1,
ParentId2,
}

#[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 {
Parent,
}

impl ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::ParentId1 => ColumnType::Integer.def(),
Self::ParentId2 => ColumnType::Integer.def(),
}
}
}

impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Parent => Entity::belongs_to(super::parent::Entity)
.from((Column::ParentId1, Column::ParentId2))
.to((super::parent::Column::Id1, super::parent::Column::Id2))
.into(),
}
}
}

impl Related<super::parent::Entity> for Entity {
fn to() -> RelationDef {
Relation::Parent.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
Loading

0 comments on commit c6e1a84

Please sign in to comment.