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 ));
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.
Error 121 is due to constraint name duplication.
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...
Related
I'm creating a mysql db based from an EER model sql script and when trying to do the conversion (all in MySQL Workbench), I'm getting the error above. My goal is to reference the adoption_entity_id on the adoption_entity table from 2 columns in attachment table (sent_from and from_to).
I have 2 tables, one of which is the attachment:
-- -----------------------------------------------------
-- Table `afth_db`.`attachment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `afth_db`.`attachment` ;
CREATE TABLE IF NOT EXISTS `afth_db`.`attachment` (
`attachment_id` BIGINT NOT NULL,
`sent_from` INT NULL,
`sent_to` INT NULL,
`attachment_description` LONGTEXT NULL,
`attachment_uploaded` DATETIME NULL,
`attachment_uploaded_by` VARCHAR(255) NULL,
`adoption_case_num` BIGINT NOT NULL,
PRIMARY KEY (`attachment_id`),
INDEX `fk_attachment_adoption_case1_idx` (`adoption_case_num` ASC) VISIBLE,
UNIQUE INDEX `attachment_id_UNIQUE` (`attachment_id` ASC) VISIBLE,
UNIQUE INDEX `adoption_case_num_UNIQUE` (`adoption_case_num` ASC) VISIBLE,
INDEX `fk_attachment_adoption_entity1_idx` (`sent_from` ASC, `sent_to` ASC) VISIBLE,
UNIQUE INDEX `sent_to_UNIQUE` (`sent_to` ASC) VISIBLE,
UNIQUE INDEX `sent_from_UNIQUE` (`sent_from` ASC) VISIBLE,
CONSTRAINT `fk_attachment_adoption_case1`
FOREIGN KEY (`adoption_case_num`)
REFERENCES `afth_db`.`adoption_case` (`case_num`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_attachment_adoption_entity1`
FOREIGN KEY (`sent_from` , `sent_to`)
REFERENCES `afth_db`.`adoption_entity` (`adoption_entity_id` , `adoption_entity_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
and the other is the adoption entity:
-- -----------------------------------------------------
-- Table `afth_db`.`adoption_entity`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `afth_db`.`adoption_entity` (
`adoption_entity_id` INT NOT NULL,
`adoption_entity_type` VARCHAR(255) NULL,
PRIMARY KEY (`adoption_entity_id`),
UNIQUE INDEX `adoption_entity_id_UNIQUE` (`adoption_entity_id` ASC) VISIBLE)
ENGINE = InnoDB
The error states in detail:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1822: Failed to add the foreign key constraint. Missing index for constraint 'fk_attachment_adoption_entity1' in the referenced table 'adoption_entity'
I'm not sure why its been giving me this issue though. I've tried several solutions from setting type to 'unique index', 'index' for fk_attachment_adoption_entity1 along with the other columns involved, but I can't seem to be able to get rid of the error. I've also tried deleting and recreating the 1-1 relationship but that didnt help either. Can anyone tell me if I'm doing something wrong here on the EER model design?
You are trying to create one foreign key reference, which spans over the two columns sent_from and sent_to to single rows in the adoption_entity table. That's not what you want. You want to create two separate foreign key references instead for the individual columns sent_from and sent_to. So the constraint parts should look like this:
CONSTRAINT `fk_attachment_adoption_entity_from`
FOREIGN KEY (`sent_from`)
REFERENCES `afth_db`.`adoption_entity` (`adoption_entity_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION),
CONSTRAINT `fk_attachment_adoption_entity_to`
FOREIGN KEY (`sent_to`)
REFERENCES `afth_db`.`adoption_entity` (`adoption_entity_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
Here is the exact SQL I attempt to execute (using SQLYog as a MySQL client on Windows):
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` INT (11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE = INNODB ;
DROP TABLE IF EXISTS `temp`;
CREATE TEMPORARY TABLE `temp` (
dish_id INT (11) NOT NULL,
user_id INT (11) NOT NULL,
UNIQUE KEY (dish_id, user_id),
FOREIGN KEY `test` (dish_id) REFERENCES test (id)
) ENGINE = INNODB ;
Here is the exact error received upon the attempt to create temp:
Error Code: 1215
Cannot add foreign key constraint
It looks to me like everything is in order - the data type and signed-ness of the two related keys is the same; the names appear to be kosher; what is going on?
What am I doing wrong? Why is the foreign key constraint failing to be created?
It's because you're trying to add a foreign key constraint to a temporary table.
From the manual:
Foreign key relationships involve a parent table that holds the
central data values, and a child table with identical values pointing
back to its parent. The FOREIGN KEY clause is specified in the child
table. The parent and child tables must use the same storage engine.
They must not be TEMPORARY tables.
http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html
EDIT:
Try it with a permanent table. It works.
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` INT (11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE = INNODB ;
DROP TABLE IF EXISTS `temp`;
CREATE TABLE `temp` (
dish_id INT (11) NOT NULL,
user_id INT (11) NOT NULL,
UNIQUE KEY (dish_id, user_id),
FOREIGN KEY `test` (dish_id) REFERENCES test (id)
) ENGINE = INNODB ;
Good day
I'm trying to solve an issue with foreign key relationship between three tables. Basically I have two secondary tables, each one has two foreign keys and each foreign key is referenced to a single primary key in the third table.
This same issue happens in a much larger database, however due to its complexity, size and possible copyright issues. I had no choice but to isolate the problem and create a replica that would result in the same issue. However if you must know, the same two tables that reference the main table People are just two in a universe of dozens of the same kind in the database in question.
My hope is that someone can point the problem and maybe provide a solution that wouldn't effect much of the current structure, so that I can apply it myself, probably to all other tables because from what I see of the database diagram, its likely they will all suffer from the same problem. Database authorship is not mine, which only adds to one's confusion when looking at the diagram.
The SQL create script:
CREATE SCHEMA IF NOT EXISTS `sampleBD` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `sampleBD` ;
-- -----------------------------------------------------
-- Table `sampleBD`.`People`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sampleBD`.`People` (
`PeopleID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(200) NOT NULL ,
`EntryDate` DATETIME NULL ,
`EntryBy` INT NULL ,
PRIMARY KEY (`PeopleID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `sampleBD`.`PeopleNumberId`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sampleBD`.`PeopleNumberId` (
`PeopleNumberIdID` INT NOT NULL AUTO_INCREMENT ,
`PeopleID` INT NOT NULL ,
`NumberId` INT(11) NOT NULL ,
`EntryDate` DATETIME NULL ,
`EntryBy` INT NULL ,
PRIMARY KEY (`PeopleNumberIdID`) ,
INDEX `PeopleID` (`PeopleID` ASC) ,
INDEX `EntryBy` (`EntryBy` ASC) ,
CONSTRAINT `PeopleID`
FOREIGN KEY (`PeopleID` )
REFERENCES `sampleBD`.`People` (`PeopleID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `EntryBy`
FOREIGN KEY (`EntryBy` )
REFERENCES `sampleBD`.`People` (`PeopleID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `sampleBD`.`PeopleCbi`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `sampleBD`.`PeopleCbi` (
`PeopleCbiID` INT NOT NULL AUTO_INCREMENT ,
`PeopleID` INT NOT NULL ,
`Cbi` INT NOT NULL ,
`EntryDate` DATETIME NULL ,
`EntryBy` INT NULL ,
PRIMARY KEY (`PeopleCbiID`) ,
INDEX `PessoaID` (`PeopleID` ASC) ,
INDEX `EntryBy` (`EntryBy` ASC) ,
CONSTRAINT `PessoaID`
FOREIGN KEY (`PeopleID` )
REFERENCES `sampleBD`.`People` (`PeopleID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `EntryBy`
FOREIGN KEY (`EntryBy` )
REFERENCES `sampleBD`.`People` (`PeopleID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
The script is able to create the first and second tables, however when it reaches the third table it returns an error code.
Error Code: 1005. Can't create table 'samplebd.peoplecbi' (errno: 121)
It's a foreign key problem, but I'm not sure how it could be fixed without altering the current structure of the table or tables involved.
Thanks for the help.
This looks like a name conflict between foreign key constraint names. Constraint names must be unique in a database, like table names are.
So just choose another name for the constraint EntryBy in the 3rd table. It will not affect any of the functionality you have.
I find innodb quite annoying when I try to design a db structure, at least compared with MyIsam, which seems to have less limitations
Say, if I want to create a simple library system.
And I have four tables.
1,table book_item, which records the book_name, author, publish time and those basic information about books
2, table book, which represents a specific real object of the book item. So a book_item object can relate to many book objects.
3, table tag, which represents a book tag. Like science, literature, architecture and so on.
4, table tag_book_item_relation, which relates tags to book_items.
So, the relations are as below.
1,we have a book item to book is one-to-many relationship
2,book_item to tag is many-to-many relationship.
Note here, engine for the table are all innodb
If I try to create the tables, it will fail:
Error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'yet_another_test.book' (errno: 121)
However, if I change the engine of book or tag_book_item_relation to MyISAM, everything will be fine.
So, I am wondering what is going wrong if I use engine innodb for tablebook and tag_book_item_relation
The sql script is here(forward engineering in MySQL workbench):
CREATE TABLE IF NOT EXISTS `yet_another_test`.`tag` (
`id` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `yet_another_test`.`book_item` (
`id` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `yet_another_test`.`tag_book_item_relation` (
`book_item_id` INT NOT NULL ,
`tag_id` INT NOT NULL ,
PRIMARY KEY (`book_item_id`, `tag_id`) ,
INDEX `fk_tag` (`tag_id` ASC) ,
INDEX `fk_book_item` (`book_item_id` ASC) ,
CONSTRAINT `fk_tag`
FOREIGN KEY (`tag_id` )
REFERENCES `yet_another_test`.`tag` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_item`
FOREIGN KEY (`book_item_id` )
REFERENCES `yet_another_test`.`book_item` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `yet_another_test`.`book` (
`id` INT NOT NULL AUTO_INCREMENT ,
`book_item_id` INT NOT NULL ,
PRIMARY KEY (`id`, `book_item_id`) ,
INDEX `fk_book_item` (`book_item_id` ASC) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) ,
CONSTRAINT `fk_book_item`
FOREIGN KEY (`book_item_id` )
REFERENCES `yet_another_test`.`book_item` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
It seems there is an issue with the "CREATE TABLE book" that the foreign key constraint fk_book_item has the same name as the constraint in tag_book_item_relation. Try using another name for the constraint in book and the CREATE TABLE should work fine.
This isn't a problem in MyISAM because they have no concept of foreign-keys and so the FK constraints are ignored.
Hope this helps!
Create your tables without the Foreign Key constraints. Although the same statements work with MyISAM engine, the constraints are silently ignored there - that is why you are not getting the errors. If you really need those constraints, then create them correctly. However, I generally tend to avoid FK constraints and implement the constraints at application level.
One problem I spot right away are the symbols of your constraints which have to be unique at DB level and you have fk_book_item both on tag_book_item_relation table and on book table
ERROR 121 says "Table creation failed because a foreign key constraint was not correctly formed. If the error message refers to error –1, table creation probably failed because the table includes a column name that matched the name of an internal InnoDB table."
Link
The Index Name and Constraint Name may be same, change that try creating the table.
This error message is saying that there is a duplicate key somewhere. It can be caused by a name conflict in a foreign key constraint; you cannot use the same foreign key name in different tables. (I don't know what other tables might be in your data base.)
Often, the error is caused by the table already existing in InnoDB's internal dictionary, even though the .frm file is gone. If that's the case, then the easiest thing to do is to do an sql dump of the data, drop the data base, recreate it, and then load the data from the dump.
Ok, so I am creating tables in MySQL with indexes and foreign keys. I use MySQL Workbench to create the tables and then have it forward engineer a SQL create script (I do better in a visual DB environment than just writing out the SQL code by hand right away).
The problem is many times when I import the sql script into mysql, I get the classic eror:
#1005 - Can't create table 'db.tablename' (errno: 121)
I've managed to figure out the problem each time, usually index/foreign key related, but now I'm starting to get irritated at having to fix it each time. I don't really understand what the problem is (especially when a MySQL product is creating sql code for its own database). Below is some code that typically causes the problem.
CREATE TABLE IF NOT EXISTS `db`.`groupMembers` (
`groupMembersID` INT NOT NULL AUTO_INCREMENT ,
`groupID` INT NOT NULL ,
`userID` INT NULL ,
PRIMARY KEY (`groupMembersID`) ,
INDEX `group` (`groupID` ASC) ,
INDEX `user` (`userID` ASC) ,
CONSTRAINT `group`
FOREIGN KEY (`groupID` )
REFERENCES `db`.`groups` (`groupsID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `user`
FOREIGN KEY (`userID` )
REFERENCES `db`.`users` (`usersID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
The error usually comes from the first INDEX definition - even when I take out the index definition, I just get the error at the the first foreign key constraint definition. I've checked, and the foreign key remote column and the local column are the same data-type and size.
"errno 121 means a duplicate key error"
Constraints must have an unique name in the database, you might wanna change your FK names. Like so, FK_groupMembers_group and FK_groupMembers_user.