sequelize: sync({alter: true}) throws "Too many keys specified"
This issue is related to #9536
What are you doing?
await sequelize.sync({ alter: true, force: false });
What do you expect to happen?
Sequelize to sync successfully.
What is actually happening?
I get this error: SequelizeDatabaseError: Too many keys specified; max 64 keys allowed
. This happens I guess after 64 times of restarting the application.
How did I workaround
There are few things that I can do
- set
force: true
. This is not practical for production environment - set
alter: false
. This is not practical when my definitions change - set MYSQL key limit, which is makes it hard for cross platform compatibility
- delete all tables, which defeats the point of use
alter
. I guess alter creates some indexes which make
Dialect: mysql Database version: XXX Sequelize version: 4.37.10 Tested with latest release: Yes (If yes, specify that version)
Note : Your issue may be ignored OR closed by maintainers if it’s not tested against latest version OR does not follow issue template.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 18
- Comments: 38 (3 by maintainers)
Commits related to this issue
- Fixed how unique keys are created with sequelize to work around this bug with `sync({alter: true})`: https://github.com/sequelize/sequelize/issues/9653 — committed to gvlasov/over.guide by gvlasov 3 years ago
- Prevent multiple unique key creation https://github.com/sequelize/sequelize/issues/9653 — committed to ex0tiq/bahamut-bot-ts by deleted user 2 years ago
old sequelize use email:{type: Sequelize.STRING, unique: true} Now, email:{type: Sequelize.STRING, unique:‘email’} this will fix the issue.
Here’s workaround that seems to fix it. If you use the approach documented here, it maintains the same index rather than adding a new one each time sequelize syncs. So instead of:
sequelize.define('user', {email: {type: Sequelize.STRING, unique:true}})
usesequelize.define('user', {email: Sequelize.STRING}, {indexes:[{unique:true, fields: ['email']}]})
The “solution” I went with is:
Now you can alter your database another 64 times until you have to do it again.
Hope I wrote it down correctly (can’t reproduce right now if I have all the steps…), please be sure to not test the first time with a production database! This is merely a dirty workaround of course, because I have no idea how this could be fixed…
The approach suggested by @cribcutkiran works for me, working with a MySQL database. Additionally, I used MySQL Workbench to delete the additional indexes created. Running sync repeatedly after that only creates a single instance of each index.
for multiple columns with unique keys,
You have to go in mysql and drop duplicate indexes in tables / models with some unique keys after errors “Too many keys specified” occurs without drop entire database.
To evitate that error occurs again it’s opportune to define unique field like this with the name of index:
email: { type: Sequelize.STRING, allowNull: false, unique: 'email'}
so sequelize don’t create a new index name at every alter table.sequelize-typescript
solutionError code
Correct code
I think u should write migration to fix database, stop using auto sync database 😄
This is not a solution for the issue.
If we can use the “true” value to make Sequelize handle the keys automatically, why would we need then to type the key name instead?
Also I’m still having this issue too, it’s almost making 3 years. My workaround is to reset the database every time I update it; it’s a very expensive task.
same too.
Thank You for this information. issue resolved.
Still an issue in ^6.19.0
I believe this is fixed in Sequelize 7.0.0-alpha.14, we have a bunch of tests to ensure indexes are not re-created by alter if they already exist: https://github.com/sequelize/sequelize/blob/main/test/integration/model/sync.test.js#L180
PR: https://github.com/sequelize/sequelize/pull/14619
i thank you for your answer !
This feature is useful when in development stage. It’s very expensive to migrate the entire database every time something is changing when you’re still developing / designing the database 😕
Still seeing this at
sequelize": "^5.19.0
Consider 👍🏼 the initial post as we often sort by the thumbs up emoji to determine what needs to be prioritized.