You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sync() with new alter mode fails on second run when using CONSTRAINTS #7606
Reacted by Eric Hill, Radon Rosborough, kukoo1, Valeriy, Miles Collier, Cyrille Gaucher, Vasiliy Klimashin, tuomash, Muli Yulzary, Abdul Vajid and 10 more
No idea if this is safe, but needed a quick fix for testing:
functiondropForeignKeyConstraints(database){//this is a hack for dev only!//todo: check status of posted github issue, https://github.com/sequelize/sequelize/issues/7606constqueryInterface=database.getQueryInterface();returnqueryInterface.showAllTables().then(tableNames=>{returnPromise.all(tableNames.map(tableName=>{returnqueryInterface.showConstraint(tableName).then(constraints=>{returnPromise.all(constraints.map(constraint=>{if(constraint.constraintType==='FOREIGN KEY'){returnqueryInterface.removeConstraint(tableName,constraint.constraintName);}}));});}));}).then(()=>database);}
Run before sync to drop all foreign keys. BRUTAL!
Reacted by benlypan, Radon Rosborough, Gold Cat, Boris Berman, Jaydeep Solanki, Felix O'Hara, Jim Camut and Matias Falcone
Reacted by Gold Cat, Bastien Griffet, Allan Ramirez, Jim Camut and Matias Falcone
@the solution would be to check for existing constraint as a method exists to do it.
As a quick fix but not a long term solution, I've written the following code in the query-interface file in the changeColumn method to check if a constraint already exists and it worked pretty well (starting line 556):
if(this.sequelize.options.dialect==='sqlite'){// sqlite needs some special treatment as it cannot change a columnreturnSQLiteQueryInterface.changeColumn.call(this,tableName,attributes,options);}else{/************************************************************ * HERE IS WHERE I CHECK FOR AN EXISTING CONSTRAINT ************************************************************/if(attributes[attributeName].references){this.showConstraint(tableName,`${tableName}_${attributeName}_foreign_idx`).then((result)=>{if(result.length===0){constquery=this.QueryGenerator.attributesToSQL(attributes);constsql=this.QueryGenerator.changeColumnQuery(tableName,query);returnthis.sequelize.query(sql,options);}});}else{constquery=this.QueryGenerator.attributesToSQL(attributes);constsql=this.QueryGenerator.changeColumnQuery(tableName,query);returnthis.sequelize.query(sql,options);}}
It is not a long term solution as some code has been duplicated.
I hope it helps
Reacted by DavidB, florent-forcity-needone, Miles Collier, Ethan, Prakash Ramasamy and Felix O'Hara
@cyrille-gaucher I added the code written but it's still giving me the SequelizeDatabaseError: relation "email_unique_idx" already exists error. I placed a few console.log() 's to see what was being called and it seems that its not reaching the if conditional and only reaching the else which is the original code if no constraint already exists which it does. Here is the code calling alter models.sequelize.sync({alter: true}).then(function() { server.listen(port, function() { console.log('Express server listening on port ' + server.address().port); }); server.on('error', onError); server.on('listening', onListening); });
Yes, here is my user model which has a unique constraint on the email attribute. It's causing the error anytime I had a new model with a relation to user or add to the user model.
@joncorrin I could not reproduce the error in your use case, I'm sorry. Maybe we are not using the same SQL engine (I'm using mysql). What are you using?
Don't know why, but it works with sync({alter: true})
(still doesn't work with unique:true property defined directly at column definition)
checked with Sequelize version 4.37.8 and 4.42.0
Reacted by heekyu, Meotimdihia, Owain Stratton, Makafui G. Dzivenu, Murat Demir, Faruk CAN and Chirag Soni
I had a similar problem. My solution was setting names to index. Probably the autogenerated name was too long. An autogenerated name is built from fields's names. After setting the name all work fine
Yes, thank you! My index name was exactly 1 character too long. I wondered why the error message cut of the last character. Now i know why.
But it think this is a bug of sequelize.
Hello everyone, I would like to ask a favor - This issue is old and has a lot of discussion and clutter, can someone please open a new issue summarizing the problem that still exists, providing an SSCCE, and linking to this issue? This will let me and others tackle the issue further.
I already had a table and I used alter so that the data is not gone. Then I created a second model and when I synced sequelize only picked the previous model. The second model was ignored. I changed it to force but still ignores the second model.
Don't know why, but it works with sync({alter: true}) (still doesn't work with unique:true property defined directly at column definition) checked with Sequelize version 4.37.8 and 4.42.0
Activity
skn3 commentedon May 4, 2017
No idea if this is safe, but needed a quick fix for testing:
Run before sync to drop all foreign keys. BRUTAL!
skn3 commentedon Jul 9, 2017
also related to #7915 and #7649
demian85 commentedon Aug 8, 2017
Any news on this?
joncorrin commentedon Aug 24, 2017
Any updates on this?
robin-garnham commentedon Oct 10, 2017
This is a bit of a pain... is anyone working on a fix?
icarusmiles commentedon Oct 18, 2017
Any news on this?
cyrille-gaucher commentedon Oct 22, 2017
@the solution would be to check for existing constraint as a method exists to do it.
As a quick fix but not a long term solution, I've written the following code in the
query-interfacefile in thechangeColumnmethod to check if a constraint already exists and it worked pretty well (starting line 556):It is not a long term solution as some code has been duplicated.
I hope it helps
joncorrin commentedon Oct 23, 2017
@cyrille-gaucher I added the code written but it's still giving me the
SequelizeDatabaseError: relation "email_unique_idx" already existserror. I placed a few console.log() 's to see what was being called and it seems that its not reaching the if conditional and only reaching the else which is the original code if no constraint already exists which it does. Here is the code calling altermodels.sequelize.sync({alter: true}).then(function() { server.listen(port, function() { console.log('Express server listening on port ' + server.address().port); }); server.on('error', onError); server.on('listening', onListening); });cyrille-gaucher commentedon Oct 23, 2017
@joncorrin You're right. This fix is only checking foreign key constraints. It needs to be improved to get it work for any constraint.
joncorrin commentedon Oct 23, 2017
@cyrille-gaucher of course. I see now. Any idea on how I could get it to check for unique constraints?
cyrille-gaucher commentedon Oct 23, 2017
@joncorrin Could you provide an model example which fails please? Also, which version of sequelize are you using?
joncorrin commentedon Oct 23, 2017
Yes, here is my user model which has a unique constraint on the email attribute. It's causing the error anytime I had a new model with a relation to user or add to the user model.
joncorrin commentedon Oct 23, 2017
@cyrille-gaucher ^^^
cyrille-gaucher commentedon Oct 23, 2017
@joncorrin I could not reproduce the error in your use case, I'm sorry. Maybe we are not using the same SQL engine (I'm using mysql). What are you using?
20 remaining items
tomascharad commentedon Jun 21, 2018
+1
gideoncatz commentedon Oct 7, 2018
+1
iwaduarte commentedon Oct 24, 2018
+1
Mercuso commentedon Dec 13, 2018
Workaround that I found:
Move all the unique indexes definitions to second argument of define method
Don't know why, but it works with
sync({alter: true})(still doesn't work with
unique:trueproperty defined directly at column definition)checked with Sequelize version 4.37.8 and 4.42.0
FatalCatharsis commentedon Mar 8, 2019
+1
Zummek commentedon Sep 30, 2019
I had a similar problem. My solution was setting names to index. Probably the autogenerated name was too long. An autogenerated name is built from fields's names. After setting the name all work fine
danielcl commentedon Oct 4, 2019
Yes, thank you! My index name was exactly 1 character too long. I wondered why the error message cut of the last character. Now i know why.
But it think this is a bug of sequelize.
papb commentedon Oct 6, 2019
Hello everyone, I would like to ask a favor - This issue is old and has a lot of discussion and clutter, can someone please open a new issue summarizing the problem that still exists, providing an SSCCE, and linking to this issue? This will let me and others tackle the issue further.
TheProgrammer21 commentedon Aug 6, 2020
Still experiencing this issue - A fix would be really important!
Ashiepaws commentedon Aug 6, 2020
Can confirm that this still causes the issue.
amaanrajput commentedon Jul 25, 2021
I already had a table and I used alter so that the data is not gone. Then I created a second model and when I synced sequelize only picked the previous model. The second model was ignored. I changed it to force but still ignores the second model.
chiragsoni81245 commentedon Jan 24, 2022
This Works for me 🙌