codegen: testing entity generation of composite foreign key (#2071)
This commit is contained in:
parent
f2bda8a4c2
commit
c6e1a84a12
@ -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(),
|
||||
}],
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
@ -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"),
|
||||
@ -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"),
|
||||
@ -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());
|
||||
@ -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"),
|
||||
@ -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"),
|
||||
@ -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());
|
||||
|
30
sea-orm-codegen/tests/compact/child.rs
Normal file
30
sea-orm-codegen/tests/compact/child.rs
Normal file
@ -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
sea-orm-codegen/tests/compact/parent.rs
Normal file
26
sea-orm-codegen/tests/compact/parent.rs
Normal file
@ -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
sea-orm-codegen/tests/compact_with_schema_name/child.rs
Normal file
30
sea-orm-codegen/tests/compact_with_schema_name/child.rs
Normal file
@ -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
sea-orm-codegen/tests/compact_with_schema_name/parent.rs
Normal file
26
sea-orm-codegen/tests/compact_with_schema_name/parent.rs
Normal file
@ -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
sea-orm-codegen/tests/expanded/child.rs
Normal file
73
sea-orm-codegen/tests/expanded/child.rs
Normal file
@ -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 {}
|
68
sea-orm-codegen/tests/expanded/parent.rs
Normal file
68
sea-orm-codegen/tests/expanded/parent.rs
Normal file
@ -0,0 +1,68 @@
|
||||
//! 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 {
|
||||
"parent"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
|
||||
pub struct Model {
|
||||
pub id1: i32,
|
||||
pub id2: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
pub enum Column {
|
||||
Id1,
|
||||
Id2,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
||||
pub enum PrimaryKey {
|
||||
Id1,
|
||||
Id2,
|
||||
}
|
||||
|
||||
impl PrimaryKeyTrait for PrimaryKey {
|
||||
type ValueType = (i32, i32);
|
||||
fn auto_increment() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter)]
|
||||
pub enum Relation {
|
||||
Child,
|
||||
}
|
||||
|
||||
impl ColumnTrait for Column {
|
||||
type EntityName = Entity;
|
||||
fn def(&self) -> ColumnDef {
|
||||
match self {
|
||||
Self::Id1 => ColumnType::Integer.def(),
|
||||
Self::Id2 => ColumnType::Integer.def(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RelationTrait for Relation {
|
||||
fn def(&self) -> RelationDef {
|
||||
match self {
|
||||
Self::Child => Entity::has_many(super::child::Entity).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::child::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Child.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
77
sea-orm-codegen/tests/expanded_with_schema_name/child.rs
Normal file
77
sea-orm-codegen/tests/expanded_with_schema_name/child.rs
Normal file
@ -0,0 +1,77 @@
|
||||
//! 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 schema_name(&self) -> Option< &str > {
|
||||
Some("schema_name")
|
||||
}
|
||||
|
||||
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 {}
|
72
sea-orm-codegen/tests/expanded_with_schema_name/parent.rs
Normal file
72
sea-orm-codegen/tests/expanded_with_schema_name/parent.rs
Normal file
@ -0,0 +1,72 @@
|
||||
//! 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 schema_name(&self) -> Option< &str > {
|
||||
Some("schema_name")
|
||||
}
|
||||
|
||||
fn table_name(&self) -> &str {
|
||||
"parent"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
|
||||
pub struct Model {
|
||||
pub id1: i32,
|
||||
pub id2: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||
pub enum Column {
|
||||
Id1,
|
||||
Id2,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
||||
pub enum PrimaryKey {
|
||||
Id1,
|
||||
Id2,
|
||||
}
|
||||
|
||||
impl PrimaryKeyTrait for PrimaryKey {
|
||||
type ValueType = (i32, i32);
|
||||
fn auto_increment() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter)]
|
||||
pub enum Relation {
|
||||
Child,
|
||||
}
|
||||
|
||||
impl ColumnTrait for Column {
|
||||
type EntityName = Entity;
|
||||
fn def(&self) -> ColumnDef {
|
||||
match self {
|
||||
Self::Id1 => ColumnType::Integer.def(),
|
||||
Self::Id2 => ColumnType::Integer.def(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RelationTrait for Relation {
|
||||
fn def(&self) -> RelationDef {
|
||||
match self {
|
||||
Self::Child => Entity::has_many(super::child::Entity).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::child::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Child.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
Loading…
x
Reference in New Issue
Block a user