Cyclic sequelize sync doesn't handle several cases in the non-cyclic version #17297
Open
3 of 6 tasks
Labels
pending-approval
Bug reports that have not been verified yet, or feature requests that have not been accepted yet
type: bug
Issue Creation Checklist
Bug Description
When running Sequelize.sync with a model graph with circular references, sequelize doesn't handle the following cases, which the non-cyclic version does:
Reproducible Example
I don't have a reproducible example, but I did debug this already. I hope the detailed analysis below will make up for that and might be enough to keep this issue from being closed 🙏
withoutForeignKeyContraints: true
and then again withforce: false, alter: true
. In this second call when creating the constraints for a multi-column primary key column with a foreign key constraint, the foreign key constraint is never created because the following code skips the call to changeColumn because the column also happens to be a primary key:sequelize/src/model.js
Lines 1385 to 1391 in 252e6d2
While debugging this, I also noticed this check here is checking a table name against a model, which is unlikely working:
sequelize/src/model.js
Line 1397 in 252e6d2
alter: true
. For the sake of brevity, lets assume postgres truncates all index names to four characters (even though it's 63). If a model has already added an index named ABCDEF, and then you add a circular model reference to your graph, you'll bump into error about sequelize failing to create an index. The following code will look up the existing indexes, see that you've already created one called ABCD, determine that you need to still create one called ABCDEF, generate SQL to create it, but then fail since postgres will automatically truncate it to ABCD, which already exists:sequelize/src/model.js
Lines 1413 to 1428 in 252e6d2
If you don't have circular references, sync() (possibly accidentally) handles this without issue automagically.
What do you expect to happen?
Sync with circular references generates correct SQL that matches the model graph
What is actually happening?
Sync generates invalid SQL for indexes and misses foreign key constraints for multi-column primary key
Environment
Would you be willing to resolve this issue by submitting a Pull Request?
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
The text was updated successfully, but these errors were encountered: