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
queryInterface.createTable() does not work with ARRAY(ENUM) with defaultValue #11285
Great SSCCEThis issue has a great SSCCE/MCVE/reprex posted and therefore deserves extra attention! :)dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).status: understoodFor issues. Applied when the issue is understood / reproducible.type: bugDEPRECATED: replace with the "bug" issue type
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
module.exports = (sequelize, DataTypes) => {
const Test = sequelize.define('Test', {
myEnum: {
type: DataTypes.ENUM,
values: ['foo', 'bar', 'baz', 'qux'],
field: 'myEnum',
allowNull: false,
// DO NOT add default value here
},
},{
freezeTableName: true, // This ensures that sequelize will not change your table names to plural ie Tests as a default behaviour
});
// Define the associations
..........
return Test;
}
Post Notes
It is a good practice to name your table with Capitalization.
Refrain from using table attributes with a similar name to the table ie if the table is named Test, don't have the field as test. Just for best practice and because sometimes the tools will have certain syntax rules and constraints that might cause weird errors.
The field was the missing link and the documentation was crappy in making that clear. Now when entering data in your table or with seeders it will look something like this(just the normal way):
{
id: 5,
testName: 'Test1',
testEmail: '[email protected]',
password: await hashedPassword(process.env.TESTER_PASSWORD), // hash user password from environment for seeder
myEnum: 'foo', // Following the syntax in the sequelize ENUM docs I linked below
createdAt: new Date().toUTCString(),
updatedAt: new Date().toUTCString(),
},
And when you use Postico to check the table Test's structure it will have the following details:-
What the scanty sequelize docs fail to mention is that similar rules for this Data type: ARRAY(ENUM) - PostgreSQL only. Apply regardless of whether you use the ARRAY(ENUM) or not. Moreso, they fail to mention that when using sync you have to explicitly specify the field in order for the sync to happen successfully. When you fail to set the field, you will see the Table structure(Postico) as above(minus the DEFAULT which will read no default )and will have the following bugs when running migrations or trying to seed the database :-
error: invalid input value for enum "enum_Test_myEnum": "" - when trying to seed the database or create a user.
Great SSCCEThis issue has a great SSCCE/MCVE/reprex posted and therefore deserves extra attention! :)dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).status: understoodFor issues. Applied when the issue is understood / reproducible.type: bugDEPRECATED: replace with the "bug" issue type
Activity
nhattran998 commentedon Aug 7, 2019
. (To watch to be notified the solve!)
jeremyputeaux commentedon Sep 3, 2019
I experimented the same problem today. Any progress on this issue?
papb commentedon Sep 4, 2019
@jetaimefrc You can subscribe by clicking the button
@jeremyputeaux No progress that I know of
rkneel123 commentedon Apr 13, 2021
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
return [
await queryInterface.sequelize.query('CREATE TYPE "enum_tenants_access" AS ENUM ('INSPECTION', 'INSTRUCTION')'),
await queryInterface.sequelize.query('ALTER TABLE tenants ADD COLUMN access "enum_tenants_access"[]'),
]
},
down: async (queryInterface, Sequelize) => {
return [
await queryInterface.removeColumn('tenants', 'access')
]
}
};
You can use raw query for array of enum type. It will work.
SqrtMinusOne commentedon Sep 27, 2021
Here is a workaround I found, if that helps anyone
github-actions commentedon Nov 8, 2021
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
LWanjiru commentedon Sep 21, 2023
If anyone is still looking for a solution to this. I encountered it and this is how I went about solving it.
Environment
I am using the following:-
SOLUTION
migration
Just adding the relevant part here so use the migration schema when doing the actual thing
model
Post Notes
It is a good practice to name your table with Capitalization.
Refrain from using table attributes with a similar name to the table ie if the table is named Test, don't have the field as test. Just for best practice and because sometimes the tools will have certain syntax rules and constraints that might cause weird errors.
The field was the missing link and the documentation was crappy in making that clear. Now when entering data in your table or with seeders it will look something like this(just the normal way):
And when you use Postico to check the table Test's structure it will have the following details:-
What the scanty sequelize docs fail to mention is that similar rules for this Data type: ARRAY(ENUM) - PostgreSQL only. Apply regardless of whether you use the ARRAY(ENUM) or not. Moreso, they fail to mention that when using sync you have to explicitly specify the field in order for the sync to happen successfully. When you fail to set the field, you will see the Table structure(Postico) as above(minus the DEFAULT which will read no default )and will have the following bugs when running migrations or trying to seed the database :-
error: invalid input value for enum "enum_Test_myEnum": "" - when trying to seed the database or create a user.
The defaultValue will NOT be set.
I hope this helps.
SqrtMinusOne commentedon Nov 15, 2023
For anyone still struggling with this, somewhere before the version 6.35.0
fieldNamewas renamed tofield.The following seems to work:
robotkoruslan commentedon Apr 17, 2024
Works with the defaultValue