Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Code: 1822. Failed to add the foreign key constaint. Missing index for constraint

Tags:

I found some threads about the error. But all the solutions doesn't work for me.

I created 2 tables a user table and one for articles. Now I want to store the user that created the article and the one who is the last modifier.

CREATE TABLE IF NOT EXISTS `testDb`.`users` (   `id` INT NOT NULL AUTO_INCREMENT,   `nickname` VARCHAR(255) NULL,   `first_name` VARCHAR(255) NULL,   `last_name` VARCHAR(255) NULL,   `e_mail` VARCHAR(255) NOT NULL,   `activated` TINYINT(1) NOT NULL DEFAULT 0,   `birth_date` DATE NULL,   `locked` TINYINT(1) NOT NULL DEFAULT 0,   `locked_date_time` DATETIME NULL,   `street` VARCHAR(255) NULL,   `street_number` VARCHAR(255) NULL,   `city` VARCHAR(255) NULL,   `postal_code` VARCHAR(255) NULL,   `country` VARCHAR(255) NULL,   `phone` VARCHAR(255) NULL,   PRIMARY KEY (`id`),   UNIQUE INDEX `user_id_UNIQUE` (`id` ASC) ) ENGINE = InnoDB AUTO_INCREMENT = 1;   CREATE TABLE IF NOT EXISTS `testDb`.`articles` (   `id` INT NOT NULL AUTO_INCREMENT,   `name` VARCHAR(255) NULL,   `description` VARCHAR(255) NULL,   `create_user` INT ZEROFILL NOT NULL,   `create_date_time` DATETIME NULL,   `last_modifie_user` INT ZEROFILL NOT NULL,   `last_modifie_date_time` DATETIME NULL,   PRIMARY KEY (`id`),   UNIQUE INDEX `article_id_UNIQUE` (`id` ASC),   INDEX `fk_articles_users1_idx` (`create_user` ASC),   INDEX `fk_articles_users2_idx` (`last_modifie_user` ASC) ) ENGINE = InnoDB AUTO_INCREMENT = 1;   ALTER TABLE `testDb`.`articles`   ADD CONSTRAINT `fk_articles_users1`     FOREIGN KEY (`create_user`)     REFERENCES `testDb`.`users` (`id`)     ON DELETE NO ACTION     ON UPDATE NO ACTION,   ADD CONSTRAINT `fk_articles_users2`     FOREIGN KEY (`last_modifie_user`)     REFERENCES `testDb`.`users` (`id`)     ON DELETE NO ACTION     ON UPDATE NO ACTION; 

I get the following error, but I didn't understand why I should have a index for that.

Error Code: 1822. Failed to add the foreign key constaint. Missing index for constraint 'fk_articles_users1' in the referenced table 'users'

I actived

SHOW ENGINE innodb STATUS; 

but this doesn't shows any erros.

like image 331
Trival Avatar asked Oct 12 '14 20:10

Trival


People also ask

What is error code 1822 in MySQL?

mysql - 1822, "Failed to add the foreign key constraint. Missing index for constraint in referenced table script - Database Administrators Stack Exchange. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Why can't I add a foreign key constraint?

The usual cause are generally a mismatch in the type of the column of the primary table and the foreign table. It can also be a mismatch in the Engine type of two tables i.e. MyISAM or InnoDB. Datatype both columns should have same datatype. int(11) on one table and smallint(5) on another will cause problem.

What does missing index for constraint mean?

The error means that the column that you're referring to, needs to be indexed, preferably a primary key or unique.

What does foreign key constraint failed mean?

The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.


Video Answer


1 Answers

create_user INT UNSIGNED ZEROFILL cannot reference id INT, because these count as different data types for purposes of foreign key reference. Make them the same data type.

The only data type difference that is permitted between columns in a foreign key relationship is length of a varchar. For example, VARCHAR(10) can reference VARCHAR(20) or vice-versa.

Any other difference in data type, size, or character set is incompatible for referential integrity.

Even having ZEROFILL on one column but not the other makes the data types incompatible.

like image 169
Bill Karwin Avatar answered Oct 15 '22 11:10

Bill Karwin