SeaQL/sea-orm

sea-orm-cli does not generate named relations

hoffic-cz opened this issue · 0 comments

Description

sea-orm-cli generate does not generate named relations. When one entity is related to a second entity by 2 different fields, the generated code calls the relations TargetEntity1 and TargetEntity2, which does not reflect the name of the relation (column).

If I manually rename the relation it will be overwritten the next time I run sea-orm-cli generate.

Steps to Reproduce

1. Create the following tables inside MySQL 8

create table persons
(
    id int unsigned primary key,
    name varchar(255) not null
);

create table mentors
(
    id int unsigned primary key,
    mentor_id int unsigned null,
    mentee_id int unsigned null,
    constraint FK_mentors_mentor foreign key (mentor_id) references persons (id),
    constraint FK_mentors_mentee foreign key (mentee_id) references persons (id)
);

2. Run sea-orm-cli

sea-orm-cli generate entity -u mysql://db_user:db_password@localhost/mentors -o entity/src --lib

Expected Behavior

The relations are named based on the column used for relating them:

// mentors.rs

...

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::persons::Entity",
        from = "Column::MentorId",
        to = "super::persons::Column::Id",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Mentor, // <-- This name here
    #[sea_orm(
        belongs_to = "super::persons::Entity",
        from = "Column::MentorId",
        to = "super::persons::Column::Id",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Mentee, // <-- This name here
}

Actual Behavior

The relations are named anonymously based on the target entity:

// mentors.rs

...

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::persons::Entity",
        from = "Column::MentorId",
        to = "super::persons::Column::Id",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Person1, // <-- This name here
    #[sea_orm(
        belongs_to = "super::persons::Entity",
        from = "Column::MentorId",
        to = "super::persons::Column::Id",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Person2, // <-- This name here
}

Reproduces How Often

10/10

Workarounds

No workarounds found.

Reproducible Example

Versions

│   └── sea-orm v0.12.9
│       ├── sea-orm-macros v0.12.6 (proc-macro)
│       │   ├── sea-bae v0.2.0 (proc-macro)
│       ├── sea-query v0.30.5
│       │   ├── sea-query-derive v0.4.1 (proc-macro)
│       ├── sea-query-binder v0.5.0
│       │   ├── sea-query v0.30.5 (*)
│   └── sea-orm-migration v0.12.6
│       ├── sea-orm v0.12.9 (*)
│       ├── sea-orm-cli v0.12.10
│       │   ├── sea-schema v0.14.1
│       │   │   ├── sea-query v0.30.5 (*)
│       │   │   └── sea-schema-derive v0.2.0 (proc-macro)
│       ├── sea-schema v0.14.1 (*)
├── sea-orm v0.12.9 (*)