Question

I use MySQL Workbench to design my database and then to export the SQL CREATE script. But when I run this script to create the database, I get an error - errno: 121.

It turns out that MySQL Workbench gives two constraints the same name, because both constraints use the same key (I have a table with primary key 'roleID' and I reference this key in two other tables).

Is there any way how I can rename the constraint directly in the designer, so when I forward engineer the SQL CREATE script, it will give no errors?

I tried double click the relation in the designer and give it a new caption, but it still generates the script with the original name.

Part of the generated script which creates the error:


CREATE  TABLE IF NOT EXISTS users.roles (
  roleID INT NOT NULL AUTO_INCREMENT ,
  ...
  PRIMARY KEY (roleID) ,
  ...);

CREATE TABLE IF NOT EXISTS users.userRoles ( ... roleID INT NOT NULL , ... CONSTRAINT roleID FOREIGN KEY (roleID ) REFERENCES users.roles (roleID ));

CREATE TABLE IF NOT EXISTS users.resourcePrivileges ( roleID INT NOT NULL , ... CONSTRAINT roleID FOREIGN KEY (roleID ) REFERENCES users.roles (roleID ));

Was it helpful?

Solution

Not sure how you ended up with that. I took MySQL WorkBench for a spin, created two tables with a FK and it created

-- -----------------------------------------------------
-- Table `mydb`.`users`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`users` (
  `idusers` INT NULL ,
  PRIMARY KEY (`idusers`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`usersRoles`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`usersRoles` (
  `users_idusers` INT NOT NULL ,
  PRIMARY KEY (`users_idusers`) ,
  CONSTRAINT `fk_usersRoles_users`
    FOREIGN KEY (`users_idusers` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Notice that the constraint has a unique name 'fk_usersRoles_users' that would not get duplicated since it uses table names. Just for fun I added another relationship between the same tables and by default I get

-- -----------------------------------------------------
-- Table `mydb`.`usersRoles`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `mydb`.`usersRoles` (
  `users_idusers` INT NOT NULL ,
  `users_idusers1` INT NOT NULL ,
  PRIMARY KEY (`users_idusers`, `users_idusers1`) ,
  INDEX `fk_usersRoles_users1` (`users_idusers1` ASC) ,
  CONSTRAINT `fk_usersRoles_users`
    FOREIGN KEY (`users_idusers` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_usersRoles_users1`
    FOREIGN KEY (`users_idusers1` )
    REFERENCES `mydb`.`users` (`idusers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Which again is a non problem (all of the above is auto generated - I have only set the table names, primary key on referenced table and added two 1:N relationships)

NOTES: Version 5.2.30.

EDIT Maybe something happened with your preferences. The default name for the fk constraints is defined on the model tab.

OTHER TIPS

Error 121 is due to constraint name duplication.solution example Generally when generating your SQL script with MYSQL forward engineering export option, to resolve the issue we need to just ensure that the "Foreign Key Names" are unique in SQL script/ schema.

When you set a constraints for Foreign keys, will not assign different name that referring same primary key of some table. So, what am trying to say is that check all your index names in all the generated scripts if there any duplication. Rename to some other. Then you can proceed...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top