Repository navigation
Description
I think there is bug in the mysql dialect addColumnQuery query generator.
What I'm doing
I'm adding a column to an existing table with a migration:
return queryInterface.addColumn('carousel', 'category_id', {
type: Sequelize.INTEGER(11).UNSIGNED,
defaultValue: null,
allowNull: true,
references: {
model: 'categories',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
});There is already another table that references the same category table which uses the field named category_id.
What I expect to happen
I would expect that the column is added with an foreign key constraint in the database.
What is actually happening?
What happens is that adding a column fails with the following error:
Executing (default): ALTER TABLE `carousel` ADD `category_id` INTEGER(11) UNSIGNED DEFAULT NULL, ADD CONSTRAINT `category_id_foreign_idx` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;
Unhandled rejection SequelizeDatabaseError: ER_DUP_KEY: Can't write; duplicate key in table '#sql-d45_999'
Why this is happening
I already did some digging in the code to see where the problem originates.
The naming convention that is used creates a foreign key is foreign key + '_foreign_idx' (see https://github.com/sequelize/sequelize/blob/master/lib/dialects/mysql/query-generator.js#L270-L277)
if (options && options.context === 'addColumn' && options.foreignKey) {
var attrName = options.foreignKey;
template += Utils._.template(', ADD CONSTRAINT <%= fkName %> FOREIGN KEY (<%= attrName %>)')({
fkName: this.quoteIdentifier(attrName + '_foreign_idx'),
attrName: this.quoteIdentifier(attrName)
});
}Adding multiple constraints with the same key name doesn't work on MySQL, as the constraint symbol value should be unique within the database.
If the CONSTRAINT symbol clause is given, the symbol value, if used, must be unique in the database. A duplicate symbol will result in an error similar to: ERROR 1022 (2300): Can't write; duplicate key in table '#sql- 464_1'. If the clause is not given, or a symbol is not included following the CONSTRAINT keyword, a name for the constraint is created automatically.
My proposal is to update the naming convention to include the table name in name of the constraint, like table name + '_' + foreign key + '_fk', to make sure that the names are unique within a database. If you want, I can create a pull request implementing this.
Dialect: mysql
Database version: 5.7.11 Homebrew
Sequelize version: sequelize 3.22
Activity
erwinhaas commentedon May 12, 2016
Would anyone be so kind to respond? I'm happy to write you a pull request, but want to check my suggestion with you first :)
janmeier commentedon May 12, 2016
Sounds good, sorry for the late reply! :)
Include tableName in MySQL fk contraint names
hagabaka commentedon Jun 1, 2016
I ran into this issue as well so I've added created a pull-request to implement the suggested fix.
Include tableName in MySQL fk contraint names
Include tableName in MySQL fk contraint names (#6008)
drunkhacker commentedon Aug 8, 2016
is there work-around for this issue? I don't have plan for upgrading to 4.0
fixed mysql 1022 error on cromtraints
Backport sequelize#6008
Backport sequelize#6008
Backport sequelize#6008
Backport #6008 (#7431)
daviscabral commentedon Sep 27, 2017
@drunkhacker I don't have plans for upgrading too - no workaround so far.
Backport sequelize#6008 (sequelize#7431)