9

I'm trying to synchronize schema changes from MySQL Workbench to my database. I'm getting the following error when it tries to create a foreign key:

Executing SQL script in server
ERROR: Error 1005: Can't create table 'tomato.#sql-2730_1b8' (errno: 121)

Here's the statement it's trying to execute:

ALTER TABLE `tomato`.`ing_allergy_ingredient` 
ADD CONSTRAINT `fk_ai_allergy`
FOREIGN KEY (`allergy_id` )
REFERENCES `tomato`.`ing_allergy` (`allergy_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION

Any ideas what this error means?

11

You will get this message if you're trying to add a constraint with a name that's already used somewhere else.

According to this post you can check your constraint like this:

If the table you're trying to create includes a foreign key constraint, and you've provided your own name for that constraint, remember that it must be unique within the database. Run this query to see if that name is in use somewhere:

SELECT
  constraint_name,
  table_name
FROM
  information_schema.table_constraints
WHERE
  constraint_type = 'FOREIGN KEY'
  AND table_schema = DATABASE()
ORDER BY
  constraint_name;
  • The foreign key didn't show up in this query, but when I looked at the table there was an index there with the same name. I believe it's a bug with the synchronization in MySQL Workbench. I downloaded the latest version (5.2.31) and the problem is gone. – BenV Jan 8 '11 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.