MySQL Workbench Forward Engineer Error 1215: Cannot add foreign key constraint -


when execute script create 2 tables, store column in customer table references id column in users table (both columns int):

set @old_unique_checks=@@unique_checks, unique_checks=0; set @old_foreign_key_checks=@@foreign_key_checks, foreign_key_checks=0; set @old_sql_mode=@@sql_mode, sql_mode='traditional,allow_invalid_dates';  create schema if not exists `part_finder` default character set utf8 ; use `part_finder` ;  -- ----------------------------------------------------- -- table `part_finder`.`users` -- ----------------------------------------------------- create table if not exists `part_finder`.`users` (   `id` int(10) unsigned not null auto_increment,   `account` mediumint(7) unsigned not null comment 'account organisation user belongs to',   `name` varchar(32) null default null,   `passenc` varchar(32) null default null,   `email` varchar(55) null default null,   `rank` decimal(1,0) null default '0',   `ip_reg` varchar(15) null default null,   `ip_visit` varchar(15) null default null,   `dtreg` int(11) not null,   `dtvisit` int(11) not null,   `visits` smallint(5) unsigned null default '0',   `pass` varchar(25) null default null,   `make_filter_on` tinyint(1) null default false,   `brand_filter_on` tinyint(1) null default false,   primary key (`id`)) engine = innodb auto_increment = 3;   -- ----------------------------------------------------- -- table `part_finder`.`customer` -- ----------------------------------------------------- create table if not exists `part_finder`.`customer` (   `id` int unsigned not null auto_increment,   `store` int(10) not null,   `name` varchar(32) null,   `address` varchar(45) null,   `address_2` varchar(45) null,   `city` varchar(15) null,   `state` tinyint unsigned not null,   `zip` char(5) null,   `phone` varchar(15) null,   `website` varchar(45) null,   `email` varchar(55) null,   primary key (`id`),   index `fk_customer_users_idx` (`store` asc),   constraint `fk_customer_users`     foreign key (`store`)     references `part_finder`.`users` (`id`)     on delete no action     on update no action) engine = innodb;   set sql_mode=@old_sql_mode; set foreign_key_checks=@old_foreign_key_checks; set unique_checks=@old_unique_checks; 

i error:

error: error 1215: cannot add foreign key constraint  -- ----------------------------------------------------- -- table `part_finder`.`customer` -- ----------------------------------------------------- create table if not exists `part_finder`.`customer` (   `id` int unsigned not null auto_increment,   `store` int(10) not null,   `name` varchar(32) null,   `address` varchar(45) null,   `address_2` varchar(45) null,   `city` varchar(15) null,   `state` tinyint unsigned not null,   `zip` char(5) null,   `phone` varchar(15) null,   `website` varchar(45) null,   `email` varchar(55) null,   primary key (`id`),   index `fk_customer_users_idx` (`store` asc),   constraint `fk_customer_users`     foreign key (`store`)     references `part_finder`.`users` (`id`)     on delete no action     on update no action) engine = innodb  sql script execution finished: statements: 6 succeeded, 1 failed  fetching view definitions in final form. nothing fetch 

i'm using innodb, i've checked data types of foreign key columns , i've made sure i'm using primary key in referenced table.

any ideas?

perhaps columns in parent tables int unsigned?

they need same data type in both tables.

foregin key constaints

reasons may foreign key constraing error:

  1. you trying reference nonexistent key on target table. make sure key on other table (it can primary or unique key),

  2. the types of columns not same (exception column on referecing table can nullable).


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -