Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated constraint name in migration for different tables #6031

Closed
xafilox opened this issue Jun 6, 2016 · 2 comments
Closed

Duplicated constraint name in migration for different tables #6031

xafilox opened this issue Jun 6, 2016 · 2 comments

Comments

@xafilox
Copy link

xafilox commented Jun 6, 2016

Hi.

What you are doing?

I have an initial migration that creates a table "offer" and a table "purchase" (among others) and defines a foreign key "offerId" at "purchase" that points to "offer" with the queryInterface.addColumn method. This works well.
Then, I'm creating a new migration that creates a new table "timeRange" that also defines a foreign key "offerId" at "timeRange" that points to "offer".

What do you expect to happen?

Both migrations should work (maybe using a combination of both table names and column names) or at least it would be great to be able to manually specify the constraint name to use in the queryInterface.addColumn method.

What is actually happening?

The second migration is failing. This is because it is creating the same constraint name "offerId_foreign_idx" for "timeRange" table that created in the first migration for "purchase" table.

This is the SQL it is generating:

ALTER TABLE `timeRange` ADD `offerId` INTEGER(11), ADD CONSTRAINT `offerId_foreign_idx` FOREIGN KEY (`offerId`) REFERENCES `offer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

And the full error:

{ [SequelizeDatabaseError: ER_CANT_CREATE_TABLE: Can't create table `TestDB`.`#sql-177b_101` (errno: 121 "Duplicate key on write or update")]
  name: 'SequelizeDatabaseError',
  message: 'ER_CANT_CREATE_TABLE: Can\'t create table `TestDB`.`#sql-177b_101` (errno: 121 "Duplicate key on write or update")',
  parent: 
   { [Error: ER_CANT_CREATE_TABLE: Can't create table `TestDB`.`#sql-177b_101` (errno: 121 "Duplicate key on write or update")]
     code: 'ER_CANT_CREATE_TABLE',
     errno: 1005,
     sqlState: 'HY000',
     index: 0,
     sql: 'ALTER TABLE `timeRange` ADD `offerId` INTEGER(11), ADD CONSTRAINT `offerId_foreign_idx` FOREIGN KEY (`offerId`) REFERENCES `offer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;' },
  original: 
   { [Error: ER_CANT_CREATE_TABLE: Can't create table `TestDB`.`#sql-177b_101` (errno: 121 "Duplicate key on write or update")]
     code: 'ER_CANT_CREATE_TABLE',
     errno: 1005,
     sqlState: 'HY000',
     index: 0,
     sql: 'ALTER TABLE `timeRange` ADD `offerId` INTEGER(11), ADD CONSTRAINT `offerId_foreign_idx` FOREIGN KEY (`offerId`) REFERENCES `offer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;' },
  sql: 'ALTER TABLE `timeRange` ADD `offerId` INTEGER(11), ADD CONSTRAINT `offerId_foreign_idx` FOREIGN KEY (`offerId`) REFERENCES `offer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;' }

Dialect: mysql (MariaDB)
Database version: 10.1.13
Sequelize version: 3.23.2

@sushantdhiman
Copy link
Contributor

I think #6008 fixed it, It will be released soon

@xafilox
Copy link
Author

xafilox commented Jun 6, 2016

Yeah, that seems to fix it. Thanks for your help!

@xafilox xafilox closed this as completed Jun 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants