Skip to content

Non-unique constraint names in MySQL #5826

Closed

Description

@erwinhaas

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.

Source: https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html

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

  1. erwinhaas commented on May 12, 2016

    @erwinhaas
    Author

    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 :)

  2. janmeier commented on May 12, 2016

    @janmeier
    Member

    Sounds good, sorry for the late reply! :)

  3. hagabaka commented on Jun 1, 2016

    @hagabaka
    Contributor

    I ran into this issue as well so I've added created a pull-request to implement the suggested fix.

  4. drunkhacker commented on Aug 8, 2016

    @drunkhacker

    is there work-around for this issue? I don't have plan for upgrading to 4.0

  5. added a commit that references this issue on Mar 27, 2017
  6. daviscabral commented on Sep 27, 2017

    @daviscabral

    @drunkhacker I don't have plans for upgrading too - no workaround so far.

  7. added a commit that references this issue on Nov 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions