mysql - Error during the creation of table due to foreign key -
i have table1 in db.
table1:
create table `product` ( `id` int(11) not null auto_increment, `typename` varchar(255) default null, `typecode` varchar(55) default null, `parent1` int(11) default null, `parent2` int(11) default null, primary key (`id`), key `parent1` (`parent1`), key `parent2` (`parent2`) ) engine=innodb auto_increment=396 default charset=latin1;
i tried create second table foreign key has reference product.typename
this creation query have used.
create table measurements ( id int(11) not null auto_increment primary key, age_group varchar(20) not null, article_type varchar(255) default null, dimension text , createdon int(11) not null, updatedon int(11) not null, createdby text not null, foreign key(article_type) references product(typename) )engine=innodb auto_increment=396 default charset=latin1;
but table creation failure following error.
error 1215 (hy000): cannot add foreign key constraint
i have done show engine innodb\g
------------------------ latest foreign key error ------------------------ 2015-05-15 19:03:28 131f71000 error in foreign key constraint of table db/measurements: foreign key(article_type) references product(typename) )engine=innodb auto_increment=396 default charset=latin1: cannot find index in referenced table referenced columns appear first columns, or column types in table , referenced table not match constraint. note internal storage type of enum , set changed in tables created >= innodb-4.1.12, , such columns in old tables cannot referenced such columns in new tables. see http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html correct foreign key definition.
can 1 point me problem , first columns concept?
referenced column should primary key
. here
foreign key(article_type) references product(typename)
you want reference typename
column not pk
.
to in way should create table producttype
this:
create table `producttype` ( `id` int(11) not null auto_increment, `typename` varchar(255) default null, `typecode` varchar(55) default null, ) engine=innodb auto_increment=396 default charset=latin1;
then can create reference this:
create table measurements ( id int(11) not null auto_increment primary key, age_group varchar(20) not null, idproducttype not null, dimension text , createdon int(11) not null, updatedon int(11) not null, createdby text not null, foreign key(idproducttype) references producttype(id) )engine=innodb auto_increment=396 default charset=latin1;
don't forget product
table. above solution suggestion, have consider table structure yourself.
Comments
Post a Comment