Reputation: 4227
phpmyadmin version: 4.1.14
db engine: InnoDB
index already set:
ALTER TABLE `tbl_name` ADD INDEX( `user_id`);
My query:
ALTER TABLE `tbl_name` ADD FOREIGN KEY (`user_id`) REFERENCES `<db_name>`.`user`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
but im still got error:
#1215 - Impossible d'ajouter des contraintes d'index externe
In mysql documentation this error:
Erreur: 1215 SQLSTATE: HY000 (ER_CANNOT_ADD_FOREIGN)
whats wrong and what i must do for solve this? Maybe i forgot something in phpmyadmin settings?
Upvotes: 2
Views: 10382
Reputation: 1002
For me it was that I used different types in the rows of parent table and child table:
I used Int
in child_table_key_id
and BigInt
in id
Upvotes: 0
Reputation: 87
if in user table id
primary key data type INT
then in post table user_id
foreign key data type should be INT NOT NULL
Upvotes: 0
Reputation: 4227
Problem solved, im add UNSIGNED to indexed column (for user_id )
`user_id` INT UNSIGNED NOT NULL ,
I think, fields with relations must have same properties
Upvotes: 6