Cli: Duplicate Foreign Key Constraint Name

Created on 29 Aug 2016  路  6Comments  路  Source: sequelize/cli

So, I've created two tables which reference the same one (say, address and a user has an address and company too). This makes me run into this problem:

Constraint created by sequelize: addressId_foreign_idx

Results: Both tables will try to use the same constraint name, which, as you might expect, results in:

code: 'ER_DUP_KEY',
errno: 1022,
sqlState: '23000',

Most helpful comment

same problem

All 6 comments

Are you configuring foreignKey or alias? Need to see more code

This is what I'm doing:

return queryInterface.addColumn(
        "companies",
        "addressId",
        {
          type: Sequelize.INTEGER,
          references: {
            model: "addresses",
            key: "id"
          },
          onDelete: "SET NULL"
        })
      .then(() => {
        return queryInterface.addColumn(
          "residentialCustomers",
          "addressId",
          {
            type: Sequelize.INTEGER,
            references: {
              model: "addresses",
              key: "id"
            },
            onDelete: "SET NULL"
          })
      });

As you can see, both use addressId. but I get that the foreign key created is the same.

same problem

+1

Is there any solution?
I've wasted all the day trying to figure out this same problem

You can now use addConstraint like methods to properly name them http://docs.sequelizejs.com/class/lib/query-interface.js~QueryInterface.html#instance-method-addConstraint

Was this page helpful?
0 / 5 - 0 ratings

Related issues

f1nnix picture f1nnix  路  4Comments

papb picture papb  路  3Comments

KaltZK picture KaltZK  路  5Comments

radglob picture radglob  路  6Comments

shomanishikawa picture shomanishikawa  路  3Comments