MySQL error no. 150 when trying to create foreign key -
i have 2 tables. 1 'subcategory' id auto-incrementing primary key.
from second table, 'product_subcategory' want have field category use 'subcategory.id' foreign key.
when using
alter table product_subcategory add constraint fk_subcategory_product foreign key (subcategory) references subcategory(id);
i receive error no. 150.
having looked error found this question answer states these conditions can lead error no 150:
1. 2 tables must engine=innodb. (can others: engine=myisam works too) 2. 2 tables must have same charset. 3. pk column(s) in parent table , fk column(s) must same data type. 4. pk column(s) in parent table , fk column(s), if have define collation type, must have same collation type; 5. if there data in foreign key table, fk column value(s) must match values in parent table pk columns. 6. , child table cannot temporary table.
so used show table status confirm tables suitable:
| name | engine | version | row_format | rows | avg_row_length | data_length | max_data_length | index_length | data_free | auto_increment | create_time | update_time | check_time | collation | checksum | create_options | comment | | subcategory | innodb | 10 | compact | 0 | 0 | 16384 | 0 | 16384 | 6291456 | null | 2013-07-31 16:37:03 | null | null | latin1_swedish_ci | null | | |
&
| name | engine | version | row_format | rows | avg_row_length | data_length | max_data_length | index_length | data_free | auto_increment | create_time | update_time | check_time | collation | checksum | create_options | comment |
| product_subcategory | innodb | 10 | compact | 0 | 0 | 16384 | 0 | 0 | 6291456 | null | 2013-07-31 16:35:04 | null | null | latin1_swedish_ci | null | | |
the difference being index_length.
the respective pk & fk column data identical can see , both tables empty.
does have suggestions may causing error no 150? or can suggest other areas me problem solve this?
edit: exact error message is:
error 1005 (hy000): can't create table 'ntm.#sql-31c_365' (errno: 150)
ddl info:
> -- mysql dump 10.13 distrib 5.5.31, debian-linux-gnu (x86_64) -- -- host: localhost database: ntm -- ------------------------------------------------------ -- server version 5.5.31-0ubuntu0.13.04.1 /*!40101 set @old_character_set_client=@@character_set_client */; /*!40101 set @old_character_set_results=@@character_set_results */; /*!40101 set @old_collation_connection=@@collation_connection */; /*!40101 set names utf8 */; /*!40103 set @old_time_zone=@@time_zone */; /*!40103 set time_zone='+00:00' */; /*!40014 set @old_unique_checks=@@unique_checks, unique_checks=0 */; /*!40014 set @old_foreign_key_checks=@@foreign_key_checks, foreign_key_checks=0 */; /*!40101 set @old_sql_mode=@@sql_mode, sql_mode='no_auto_value_on_zero' */; /*!40111 set @old_sql_notes=@@sql_notes, sql_notes=0 */; -- -- table structure table `product` -- drop table if exists `product`; /*!40101 set @saved_cs_client = @@character_set_client */; /*!40101 set character_set_client = utf8 */; create table `product` ( `id` int(11) not null auto_increment, `category` int(11) default null, `name` varchar(255) default null, `price` int(11) default null, `tax` double default null, `weight` int(11) default null, `size` varchar(255) default null, `discount` varchar(255) default null, primary key (`id`), key `fk_product_category` (`category`), constraint `fk_product_category` foreign key (`category`) references `category` (`id`) ) engine=innodb default charset=latin1; /*!40101 set character_set_client = @saved_cs_client */; -- -- table structure table `product_subcategory` -- drop table if exists `product_subcategory`; /*!40101 set @saved_cs_client = @@character_set_client */; /*!40101 set character_set_client = utf8 */; create table `product_subcategory` ( `product` int(11) not null, `subcategory` int(11) default null, primary key (`product`), constraint `fk_product_subcategory` foreign key (`product`) references `product` (`id`) ) engine=innodb default charset=latin1; /*!40101 set character_set_client = @saved_cs_client */; -- -- table structure table `subcategory` -- drop table if exists `subcategory`; /*!40101 set @saved_cs_client = @@character_set_client */; /*!40101 set character_set_client = utf8 */; create table `subcategory` ( `id` int(11) not null default '0', `parent` int(11) default null, `name` varchar(255) default null, `text` longtext, `summary` longtext, `image` varchar(255) default null, `featured` tinyint(1) default null, primary key (`id`), key `fk_sc_parent_id` (`parent`), constraint `fk_sc_parent_id` foreign key (`parent`) references `category` (`id`) ) engine=innodb default charset=latin1; /*!40101 set character_set_client = @saved_cs_client */; /*!40103 set time_zone=@old_time_zone */; /*!40101 set sql_mode=@old_sql_mode */; /*!40014 set foreign_key_checks=@old_foreign_key_checks */; /*!40014 set unique_checks=@old_unique_checks */; /*!40101 set character_set_client=@old_character_set_client */; /*!40101 set character_set_results=@old_character_set_results */; /*!40101 set collation_connection=@old_collation_connection */; /*!40111 set sql_notes=@old_sql_notes */;
i tried , found mistyped capitalization in name of subcategory
. table names in mysql case-sensitive when use os has case-sensitive filenames.
mysql> select version(); +---------------+ | version() | +---------------+ | 5.6.12-56-log | +---------------+ mysql> alter table product_subcategory add constraint fk_subcategory_product foreign key (subcategory) references subcategory(id); error 1215 (hy000): cannot add foreign key constraint mysql> alter table product_subcategory add constraint fk_subcategory_product foreign key (subcategory) references subcategory(id); query ok, 0 rows affected (0.33 sec)
see http://dev.mysql.com/doc/refman/5.6/en/identifier-case-sensitivity.html more details.
Comments
Post a Comment