Skip to content

Create duplicated foreign key (constraint) when using PostgreSQL #2815

@qingyiwebt

Description

@qingyiwebt

Description

After that #2802 is solved, I found that sea-orm creates duplicated foreign keys (constraints), which causes error(s).
Log is too long and I uploaded it to https://gist.github.com/qingyiwebt/e01373743f66e81b48db7a76eff786ae, the code of entities is the same as #2802 (comment)

Steps to Reproduce

  1. Sync entities
  2. Sync entities again without any code changes
  3. sea-orm creates duplicated foreign keys, which causes constraint "..." for relation "...." already exists

Expected Behavior

sea-orm does nothing, and no errors are produced.

Actual Behavior

Get error:
constraint "..." for relation "...." already exists

Reproduces How Often

Always

Workarounds

I found that,

if db.get_database_backend() != DbBackend::Sqlite {
for foreign_key in self.table.get_foreign_key_create_stmts().iter() {
let mut key_exists = false;
for existing_key in existing_table.get_foreign_key_create_stmts().iter() {
if compare_foreign_key(foreign_key, existing_key) {
key_exists = true;
break;
}
}
if !key_exists {
db.execute(foreign_key).await?;
}
}
}

compare_foreign_key(foreign_key, existing_key) never be true and causes db.execute(foreign_key).await?; is always executed.

Image Image

Versions

├── sea-orm v2.0.0-rc.18 (/path/to/my/Workspace/WORK_sea-orm)
│   ├── sea-orm-macros v2.0.0-rc.18 (proc-macro) (/path/to/my/Workspace/WORK_sea-orm/sea-orm-macros)
│   │   ├── sea-bae v0.2.1 (proc-macro)
│   ├── sea-query v1.0.0-rc.17
│   │   ├── sea-query-derive v1.0.0-rc.11 (proc-macro)
│   ├── sea-query-sqlx v0.8.0-rc.9
│   │   ├── sea-query v1.0.0-rc.17 (*)
│   ├── sea-schema v0.17.0-rc.14 (/path/to/my/Workspace/sea-schema)
│   │   ├── sea-query v1.0.0-rc.17 (*)
│   │   ├── sea-query-sqlx v0.8.0-rc.9 (*)
│   │   ├── sea-schema-derive v0.3.0 (proc-macro) (/path/to/my/Workspace/sea-schema/sea-schema-derive)

To debug sea-orm and its dependencies, I have cloned sea-orm and sea-schema.

sea-schema has the same version as SeaQL/sea-schema@4c49725,
and sea-orm has the same version as 7ffe295

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions