MySQL foreign key constraint fails -


how possible:

mysql> select id posts; +----+ | id | +----+ |  1 | +----+ 1 row in set (0.00 sec)  mysql> select id tags; +----+ | id | +----+ |  1 | +----+ 1 row in set (0.00 sec)  mysql> insert pots_x_tags values(1,1); error 1452 (23000): cannot add or update child row: foreign key constraint fails (`posts_x_tags`, constraint `posts_x_tags_ibfk_2` foreign key (`tag_id`) references `tag` (`id`) on delete cascade) 

here tables (many-to-many):

create table `post_tag_map` (   `post_id` int(11) not null,   `tag_id` int(11) not null,   primary key (`post_id`,`tag_id`),   foreign key (`post_id`) references posts(`id`) on delete cascade,   foreign key (`tag_id`) references tag(`id`) on delete cascade )   create table `tags` (   `id` int(11) not null auto_increment,   `tag` varchar(255) not null,   primary key (`id`),   unique (`tag`) ) create table `posts` (   `id` int(11) not null auto_increment,   `title` varchar(255) character set latin1 not null,   `body` text character set latin1,   primary key (`id`) ) 

on surface, appear foreign key on post_tag_map.tag_id column points table called tag opposed table called tags.


Comments