sequelize: sync() with new alter method fails with dataType enum
What you are doing?
'use strict';
const Sequelize = require('sequelize');
//first connect to the database
const database = new Sequelize('database', 'user', 'password', {
dialect: 'postgres',
host: '127.0.0.1',
port: 5432,
pool: {
max: 5,
min: 0,
idle: 10000
},
});
return database.authenticate()
.then(() => {
//success connecting,
console.log('database connected');
return database;
})
.then(database => {
const containerModel = database.define('TestContainer', {
option: {
type: Sequelize.DataTypes.ENUM('enum1', 'enum2', 'enum3'),
defaultValue: 'enum1',
allowNull: false
},
});
return database.sync({
alter: true,
force: false,
});
})
.catch(err => {
//failed to connect
console.error(err);
process.exit(1);
});
What do you expect to happen?
Being able to run the example multiple times without fail
What is actually happening?
On second run, an error occurs:
SequelizeDatabaseError: type “enum_TestContainer_option” already exists
Dialect: postgres Database version: 9.6.2 Sequelize version: git master
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 22
- Comments: 56 (9 by maintainers)
Links to this issue
Commits related to this issue
- fix(postgres): sync with alter method fails with dataType enum (#7649) — committed to aristov/sequelize by aristov 4 years ago
- fix(postgres): sync with alter method fails with dataType enum (#7649) — committed to aristov/sequelize by aristov a year ago
- fix(postgres): sync with alter method fails with dataType enum (#7649) — committed to aristov/sequelize by aristov a year ago
- fix(postgres): sync with alter method fails with dataType enum (#7649) — committed to aristov/sequelize by aristov a year ago
- fix(postgres): sync with alter method fails with dataType enum (#7649) — committed to aristov/sequelize by aristov a year ago
- fix(postgres): sync with alter method fails with dataType enum (#7649) — committed to aristov/sequelize by aristov a year ago
- fix(postgres): sync with alter method fails with dataType enum (#7649) — committed to aristov/sequelize by aristov a year ago
- fix(postgres): sync with alter method fails with dataType enum (#7649) — committed to aristov/sequelize by aristov a year ago
- fix(postgres): sync with alter method fails with dataType enum (#15738) * fix(postgres): sync with alter method fails with dataType enum (#7649) * Fixing issues after code review * Fixing issue... — committed to sequelize/sequelize by aristov a year ago
This https://github.com/sequelize/sequelize/issues/7649#issuecomment-316942602 helps. I use this workaround while a new patch version containing the fix is in progress:
Require this script before the first Sequelize require statement:
Hi @papb
I still have a problem. versoin “5.19.8”
If anyone is still facing this issue, here’s the workaround I’m using.
Instead of doing this:
I instead do this:
There you have it, all the enum validation of DataTypes.ENUM, without the sync problem. 😄
Note: If anybody is motivated to test out this workaround for high-volume queries, please share back whether it has any measurable performance implications.
This is still an issue and we need a proper fix for this
🎉 This issue has been resolved in version 6.29.1 🎉
The release is available on:
Your semantic-release bot 📦🚀
The issue still not fixed.
I have an open PR that has changes related to this. I can try to fix there, but just waiting on some feedback before I put more time into https://github.com/sequelize/sequelize/pull/11514
Why isn’t this being fixed?
Thanks, But then it wouldn’t define it as Enum on the postgres DB, will it have any performence / issues because of that?
And the problem isn’t fixed yet, on version 6.11
still have this issue
I still have this issue
Can someone help with this issue? It has been years now
I’ve this exact issue
This issue, together with #7606 effectively makes the alter option useless for anything beyond unrelated tables with no enums.
This one seems to stem from this bunch of statements:
Some kind of check should be added to check if the type exists before executing CREATE TYPE.
still have this issue
Hey, is this issue fixed yet? Because there seems to be no comments after March. @skn3
I’m also dealing with this issue.
Also, I had to change @aristov version:
Was changed to this:
Yes, but you may not like it: Don’t use sync with alter, use migrations instead. Sync with alter is no good in production anyway; might as well get good at migrations early on.
I told you youmay not like it 😃.
Eric
5 years and the same issue? Is there a plan to fix this? let us know please so we can workaround or wait.
I encountered this problem as well. From what I can tell, line 292 on
lib/dialects/postgres/query-generator.js
(inchangeColumnQuery
function) is most likely the offending line.It’s a blind call to
pgEnum
(which generates aCREATE TYPE ... AS ENUM(...)
statement), without checking if the type’s name has already been taken.Unfortunately, I don’t know sequelize internals or postgres well enough to suggest changes or come up with a workaround.