Repository navigation
Closed
Closed
Copy link
Labels
existing workaroundFor issues. There is a known workaround for this issue.type: bugDEPRECATED: replace with the "bug" issue type
Description
Issue Description
When there is the unique: true property for any model field, sequelize creates a new unique constraint and index on each sequelize.sync({ alter: true })
What are you doing?
Create any model with a unique field like that:
public initModel({ sequelize }: { sequelize: Sequelize }): ModelCtor<ExampleModel> {
const exampleModel = sequelize.define<ExampleModel>(
"ExampleModel",
{
id: {
allowNull: false,
defaultValue: DataTypes.UUIDV1,
primaryKey: true,
type: DataTypes.UUID,
},
exampleField: {
unique: true,
allowNull: false,
type: DataTypes.STRING,
},
);
return exampleModel;
}Then call sequelize.sync({ alter: true }).
What do you expect to happen?
Only single unique constraint and index are created after the each sequelize.sync({ alter: true }) call
What is actually happening?
For each sequelize.sync({ alter: true }) call there will be a new constraint named ExampleModel_exampleField_key1 (the next will be ExampleModel_exampleField_key2 and so on) and a new index called the same.
Environment
- Sequelize version: 6.3.5
- Node.js version: 14.15.0
- Operating System: WSL2 Ubuntu 20.04
- If TypeScript related: TypeScript version: 4.0.2
Issue Template Checklist
How does this problem relate to dialects?
- I think this problem happens regardless of the dialect.
- I think this problem happens only for the following dialect(s):
- I don't know, I was using PostgreSQL, with connector library version 8.3.3 and database version 12.4
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Reacted by leledumbo549, Alexis Quezada, Yael Chavoya, Tiago Wippel, MeFelixWang, Panagiotis Gkournelos, kreinholt, Ego Brain, T-Matteo, Wesley Griffing and 22 more
Metadata
Metadata
Assignees
Labels
existing workaroundFor issues. There is a known workaround for this issue.type: bugDEPRECATED: replace with the "bug" issue type
Activity
yusufkhan07 commentedon Dec 5, 2020
Facing the same issue.
sandy912 commentedon Jan 21, 2021
Facing the same issue with dialect Postgres. I am using Postgres 13. Any workaround would be much appreciated.
tiagowippel commentedon Feb 23, 2021
Facing the same issue here too.
gkourpa commentedon Mar 21, 2021
Facing the same issue.
FuketsuBaka commentedon May 12, 2021
Facing the same issue. Postgres.
EgoBrainProgrammer commentedon May 18, 2021
Facing the same issue. Postgres.
LucasRosaWP commentedon May 26, 2021
Facing the same issue.
ingrocha commentedon May 27, 2021
Facing the same issue.
abtisam commentedon May 29, 2021
facing same issue today which exhaust table default indices
Error: Too many keys specified; max 64 keys allowed
My workaround: remove all duplicate indices for now by using given below query in MySQL db.
ALTER TABLE table_name
DROP INDEX phone_2, -- phone_2 is index name
DROP INDEX phone_3 -- phone_3 is index name
;
IJlima commentedon Jun 14, 2021
I'm getting duplicate foreign key constraint when using sequelize.sync({ alter: true })
mkantz84 commentedon Jun 23, 2021
Same issue here
waqardeveloper commentedon Jul 7, 2021
same issue
abtisam commentedon Jul 9, 2021
Have you tried the above workaround ? It works for me
12 remaining items
g43ld commentedon Dec 15, 2021
Hello, faced same issue (but with mysql db), the workaround consisting of replacing
unique: truein model's field config byindexes: [{ unique: true, fields: ["YOUR_FIELD_NAME_HERE"] }]worked for me ! No more duplicate keys on sequelize sync (alter).gkourpa commentedon Dec 15, 2021
The workaround
indexes: [{ unique: true, fields: ["YOUR_FIELD_NAME_HERE"] }]worked for me as well on my MySQL database!Seblor commentedon Dec 28, 2021
The workaround does not seem to work for me. Possibly because I am using a composite index with SQLite.
Here is my model:
Synchronizing with
alter: truegives the errorshennong1943 commentedon Jan 11, 2022
Facing the same issue.
leonardssh commentedon Feb 8, 2022
@Seblor try:
Works for me.
Seblor commentedon Feb 8, 2022
@leonardssh Wouldn't that create 3 unique indexes ?
What I need is to have a single index that is based on the 3 fields.
leonardssh commentedon Feb 8, 2022
@Seblor
Also works with only single index:
Seblor commentedon Feb 8, 2022
@leonardssh I don't see the difference with the code snippet I posted.
It works, but throws an error when synchronizing with
{alter: true}, which is the current issue.leonardssh commentedon Feb 8, 2022
I use
alter: truetoo and it works perfectly.My env:
taozhi8833998 commentedon May 13, 2022
ephys commentedon Apr 3, 2023
While unique constraints/keys and unique indexes are different things, in practice creating one creates the other. In Sequelize, defining a unique index using the
uniqueproperty of an attribute, or using theindexesoption of the model does the same thingIt's more obvious in Sequelize 7 where
YourModel.getIndexes()return indexes created by theunique&index(this one is new) attribute options & theindexestable option, which is what we use then to create the unique constraint/indexesbhuwan-pandey commentedon Feb 6, 2025
I finally figured out the issue. I have been trying for days to figure out this and now i am here.
In ./node_modules/sequelize/lib/model-manager.js, inside the function
getModelsTopoSortedByForeignKey, there's this code:try { sorted = sorter.sort(); } catch (e) { if (!e.message.startsWith("Cyclic dependency found.")) { throw e; } return null; }when i debugged and debugged and debugged, what i came to find was there was stupid cyclic dependency error in my model's relation. the
einside thecatchblock there helped me figure out but i am surprised to see that an error would get thrown in there except for a cyclic dependency. the above function is called through .node_modules/sequelize/lib/sequelize.js, inside of the functionsyncand in these lines:const models = this.modelManager.getModelsTopoSortedByForeignKey(); if (models == null) { return this._syncModelsWithCyclicReferences(options); }Because due to cyclic dependency the
this.modelManager.getModelsTopoSortedByForeignKey();would return null and thus executethis._syncModelsWithCyclicReferences(options);.When i synced the models, no cyclic dependency error were thrown and i believe sequelize is supposed throw such errors.
When you debug the
einside the catch block ofgetModelsTopoSortedByForeignKey, you get the cause/relation of the dependency and just avoid it. at least that's what i did.Now my syncing WORKS like a charm like before !!! I would rather avoid cyclic dependency than to work with it and get lost.
Sequelize team, please fix the logic behind it or at least make it throw such error. it would save someone's life 😄