Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's with "id_" and "id" and "id" causes the Error 1005: Can't create table 'xxx.xxx' (errno: 121)
    primarykey
    data
    text
    <p>I am new to MySQL and was creating a complex EER diagram. After creation, I "Forward Engineer..." the model and hit the dreaded ERROR: Error 1005: Can't create table 'xxx.xxx' (errno: 121). I created another simplified model of two tables with a 1:M relationship, but used "id_" for the primary and foreign key names. That worked. </p> <pre><code>i.e. TABLE_A id_TABLE_A INT TABLE_B id_TABLE_B INT id_TABLE_A INT </code></pre> <p>where, there is a one to many relationship between TABLE_A [1]--&lt;[M] TABLE_B, and TABLE_B.id_TABLE_A is the foreign key</p> <p>Looking at my complex EER diagram, I noticed I used "id" with no underscore for the primary and foreign key names. I inserted the underscore after "id" and Forward Engineer'ed the model and it worked with no errors. So, here are two simple example models, one with the "id_" and the other with "id". The "id_" DOES NOT cause the Error 1005 and the "id" causes the Error 1005. <strong>Anyone with an idea as to why this anomaly happens with MySQL?</strong></p> <p>=============================================================================== <strong>Good Model:</strong></p> <pre><code>SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ; USE `mydb` ; DROP TABLE IF EXISTS `mydb`.`TABLE_A` ; CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_A` ( `id_TABLE_A` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY (`id_TABLE_A`) , UNIQUE INDEX `id_TABLE_A_UNIQUE` (`id_TABLE_A` ASC) ) ENGINE = InnoDB; DROP TABLE IF EXISTS `mydb`.`TABLE_B` ; CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` ( `id_TABLE_B` INT NOT NULL AUTO_INCREMENT , `id_TABLE_A` INT NOT NULL , PRIMARY KEY (`id_TABLE_B`) , UNIQUE INDEX `id_TABLE_B_UNIQUE` (`id_TABLE_B` ASC) , INDEX `id_TABLE_A` (`id_TABLE_A` ASC) , CONSTRAINT `id_TABLE_A` FOREIGN KEY (`id_TABLE_A` ) REFERENCES `mydb`.`TABLE_A` (`id_TABLE_A` ) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB; SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; </code></pre> <p><strong>RESULT:</strong></p> <pre><code>Build is successful. </code></pre> <p>===============================================================================</p> <p><strong>Bad Model:</strong></p> <pre><code>SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ; USE `mydb` ; DROP TABLE IF EXISTS `mydb`.`TABLE_A` ; CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_A` ( `idTABLE_A` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY (`idTABLE_A`) , UNIQUE INDEX `idTABLE_A_UNIQUE` (`idTABLE_A` ASC) ) ENGINE = InnoDB; DROP TABLE IF EXISTS `mydb`.`TABLE_B` ; CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` ( `idTABLE_B` INT NOT NULL AUTO_INCREMENT , `idTABLE_A` INT NOT NULL , PRIMARY KEY (`idTABLE_B`) , UNIQUE INDEX `idTABLE_B_UNIQUE` (`idTABLE_B` ASC) , INDEX `idTABLE_A` (`idTABLE_A` ASC) , CONSTRAINT `idTABLE_A` FOREIGN KEY (`idTABLE_A` ) REFERENCES `mydb`.`TABLE_A` (`idTABLE_A` ) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB; SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; </code></pre> <p><strong>RESULT:</strong></p> <pre><code>Executing SQL script in server ERROR: Error 1005: Can't create table 'mydb.table_b' (errno: 121) CREATE TABLE IF NOT EXISTS `mydb`.`TABLE_B` ( `idTABLE_B` INT NOT NULL AUTO_INCREMENT , `idTABLE_A` INT NOT NULL , PRIMARY KEY (`idTABLE_B`) , UNIQUE INDEX `idTABLE_B_UNIQUE` (`idTABLE_B` ASC) , INDEX `idTABLE_A` (`idTABLE_A` ASC) , CONSTRAINT `idTABLE_A` FOREIGN KEY (`idTABLE_A` ) REFERENCES `mydb`.`TABLE_A` (`idTABLE_A` ) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB SQL script execution finished: statements: 8 succeeded, 1 failed </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload