vapor / fluent-kit

Swift ORM (queries, models, and relations) for NoSQL and SQL databases
MIT License
218 stars 116 forks source link

Default `constraintIdentifier` algorithm is corrupted, but it’s breaking to fix #508

Closed stevapple closed 2 months ago

stevapple commented 2 years ago

Describe the bug

The current implementation of constraintIdentifier for foreign key prefixed all local and foreign fields with the local table name, but the output is not unique in fact.

To Reproduce

db.schema("a")
    .field("id", .uuid, .identifier(auto: false))
    .foreignKey("id", references: "b", "id")
    .foreignKey("id", references: "c", "id")
    .create()

Both constraint has the identifier fk:a.id+a.id, so the creation would fail.

Expected behavior

For the example above, the identifiers should be:

Additional context

Fixing the implementation is easy, but this affects all existing foreign keys without explicit naming, and users cannot delete them by deleteConstraint(_ constraint: DatabaseSchema.ConstraintDelete), so it is breaking and should be noticed in the upgrading guide.

gwynne commented 2 months ago

As you noted, this is breaking to fix; best I can do is say it will be handled better in Fluent 5.