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

Foreign key constraint collision when multiple keys exist between the same two tables #405

Closed
hlb8122 opened this issue Dec 26, 2021 · 3 comments · Fixed by #417
Closed
Assignees
Milestone

Comments

@hlb8122
Copy link

hlb8122 commented Dec 26, 2021

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::account::Entity",
        from = "Column::Member1",
        to = "super::account::Column::Id"
    )]
    Member1,
    #[sea_orm(
        belongs_to = "super::account::Entity",
        from = "Column::Member2",
        to = "super::account::Column::Id"
    )]
    Member2,
}

generates

  CONSTRAINT "fk-team2-account" FOREIGN KEY ("member1") REFERENCES "account" ("id"),
  CONSTRAINT "fk-team2-account" FOREIGN KEY ("member2") REFERENCES "account" ("id")

which results in

error returned from database: constraint "fk-team2-account" for relation "team2" already exists
@billy1624
Copy link
Member

Hey @hlb8122, I was thinking a new naming scheme for foreign keys.

What do you think if we employ the scheme below?

How about FK_TABLENAME_COLUMNNAME?
Keep It Simple Stupid whenever possible.

https://stackoverflow.com/a/199504/7059723

@hlb8122
Copy link
Author

hlb8122 commented Jan 4, 2022

Does this scheme prevent colisions in all cases?

What I was looking for when I noticed the collision was a way to do manual naming. I'm not sure whether this is possible in addition to this fix?

@billy1624
Copy link
Member

billy1624 commented Jan 5, 2022

Does this scheme prevent colisions in all cases?

Not all cases, it's possible to create two foreign keys on the same local column and referencing two different foreign columns. But I would say this seems unlikely to happen in real life.

CONSTRAINT "fk-team2-member1" FOREIGN KEY ("member1") REFERENCES "account1" ("id")
CONSTRAINT "fk-team2-member1" FOREIGN KEY ("member1") REFERENCES "account2" ("id")

What I was looking for when I noticed the collision was a way to do manual naming. I'm not sure whether this is possible in addition to this fix?

Yeah, it's possible to perform manual naming on RelationDef, I can add that.
See d267ed6

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