Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect entity generation for relation with composite keys #1690

Closed
affanshahid opened this issue Jun 4, 2023 · 11 comments · Fixed by #1693 or SeaQL/sea-schema#118
Closed

Incorrect entity generation for relation with composite keys #1690

affanshahid opened this issue Jun 4, 2023 · 11 comments · Fixed by #1693 or SeaQL/sea-schema#118
Assignees

Comments

@affanshahid
Copy link

affanshahid commented Jun 4, 2023

Description

The generated entity is incorrect if the relation has a composite key.

Steps to Reproduce

First, create a table with a composite primary key.
Second, create another table with two foreign keys columns pointing to the original table.
Third, run the cli to generate entities.

Expected Behavior

The generated entities should have the correct values for the from and to attributes in the generated Relation enum.

Actual Behavior

The generated entity randomly points to one of the primary key columns

Reproduces How Often

Always, reproducible.

Workarounds

Manually updating the attributes to the correct value works fine.

Versions

│   ├── sea-orm v0.11.3
│   │   ├── sea-orm-macros v0.11.3 (proc-macro)
│   │   ├── sea-query v0.28.5
│   │   │   ├── sea-query-derive v0.3.0 (proc-macro)
│   │   ├── sea-query-binder v0.3.1
│   │   │   ├── sea-query v0.28.5 (*)
│   │   ├── sea-strum v0.23.0
│   │   │   └── sea-strum_macros v0.23.0 (proc-macro)
├── sea-orm v0.11.3 (*)
@billy1624
Copy link
Member

Hey @affanshahid, thanks for the report! I just checked and indeed it's happening. This PR should fix the issue. Feel free to check

@affanshahid
Copy link
Author

Now, it seems to be generating duplicate entries for the columns. Here is the table structure for the original table:

CREATE TABLE "public"."project" (
    "id" text NOT NULL,
    "team_id" text NOT NULL,
    "weekends" _text NOT NULL,
    PRIMARY KEY ("id","team_id")
);

The structure for the table with the FKs:

CREATE TABLE "public"."share" (
    "id" uuid NOT NULL,
    "team_id" text NOT NULL,
    "project_id" text NOT NULL,
    "created_at" timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "share_team_id_project_id_fkey" FOREIGN KEY ("team_id","project_id") REFERENCES "public"."project"("team_id","id") ON DELETE CASCADE,
    PRIMARY KEY ("id")
);

The generated code:

use sea_orm::entity::prelude::*;
use serde::Serialize;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, poem_openapi :: Object)]
#[sea_orm(table_name = "share")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: Uuid,
    #[sea_orm(column_type = "Text")]
    pub team_id: String,
    #[sea_orm(column_type = "Text")]
    pub project_id: String,
    pub created_at: DateTimeWithTimeZone,
    pub updated_at: DateTimeWithTimeZone,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::project::Entity",
        from = "(Column::TeamId, Column::TeamId, Column::ProjectId, Column::ProjectId)",
        to = "(super::project::Column::Id, super::project::Column::TeamId, super::project::Column::Id, super::project::Column::TeamId)",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    Project,
}

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

impl ActiveModelBehavior for ActiveModel {}

@mccormickt
Copy link

👋 Really appreciate the SeaQL community's work on SeaORM, thanks for working on this! It seems the associated PR has been approved for some time now, any update on when we'd be able to see this landed?

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 20, 2023

Thanks for the nudge. I'll cross check with @billy1624 We should be able to include it in the next minor release, which should come out at the end of this week.

@lucasmerlin
Copy link

I tested the most recent version of sea-orm-cli and I have the same problems as @affanshahid. For my entity with foreign key (id, version) it generates the following:

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
  #[sea_orm(
    belongs_to = "super::other_entity::Entity",
    from = "(Column::Id, Column::Id, Column::Version, Column::Version)",
    to = "(super::other_entity::Column::Id, super::other_entity::Column::Version, super::other_entity::Column::Version, super::other_entity::Column::Id)",
    on_update = "NoAction",
    on_delete = "NoAction"
  )]
  OtherEntity,
}

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 24, 2023

@lucasmerlin can you share your schema for investigation?

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 25, 2023

@lucasmerlin Thanks for the report. Actually we observed that too, so would be able to investigate.

@lucasmerlin
Copy link

Should this issue be reopened until this is fixed? I could also create a new one

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 2, 2023

Right. I think we need to patch SeaSchema as well.

@tyt2y3 tyt2y3 reopened this Oct 2, 2023
@ronanM
Copy link

ronanM commented Nov 25, 2023

With the MusicBrainz Postgres DB (https://github.com/metabrainz/musicbrainz-docker), the relations are not generated.

@hlgltvnnk
Copy link

Hi, do you have any updates on this? Can you please tell me if this will be fixed in the next release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants