Question

I have this weird issue with creation of foreign key.

Given 2 tables:

CREATE TABLE IF NOT EXISTS `groupdeals` (
  `groupdeals_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `product_id` int(10) NOT NULL,
  `merchant_id` int(11) NOT NULL,
  `minimum_qty` int(11) NOT NULL,
  `maximum_qty` int(11) NOT NULL,
  `target_met_email` int(11) NOT NULL,
  `coupon_barcode` text NOT NULL,
  `coupon_merchant_address` int(11) NOT NULL,
  `coupon_merchant_contact` int(11) NOT NULL,
  `coupon_expiration_date` date DEFAULT NULL,
  `coupon_price` int(11) NOT NULL,
  `coupon_fine_print` int(11) NOT NULL,
  `coupon_highlights` int(11) NOT NULL,
  `coupon_merchant_description` int(11) NOT NULL,
  `coupon_business_hours` int(11) NOT NULL,
  `coupon_merchant_logo` int(11) NOT NULL,
  `coupon_additional_info` text NOT NULL,
  `position` int(11) NOT NULL,
  PRIMARY KEY (`groupdeals_id`),  
  KEY `groupdeals_id` (`groupdeals_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

and

CREATE TABLE IF NOT EXISTS `groupdeals_coupons` (
  `coupon_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `groupdeals_id` int(11) NOT NULL,
  `order_item_id` int(11) NOT NULL,
  `coupon_code` varchar(255) NOT NULL DEFAULT '',
  `redeem` varchar(255) NOT NULL DEFAULT '',
  `status` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`coupon_id`),
  KEY `fk_groupdeals_coupons_groupdeals1_idx` (`groupdeals_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

I try to add foreign key by executing following query:

ALTER TABLE `groupdeals_coupons` 
ADD CONSTRAINT `fk_groupdeals_coupons_groupdeals1`
  FOREIGN KEY (`groupdeals_id`)
  REFERENCES `groupdeals` (`groupdeals_id`)
  ON DELETE CASCADE
  ON UPDATE CASCADE;

All I receive is Error:

#1215 - Cannot add foreign key constraint

Show engine innodb status

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2013-08-17 13:47:49 7ff5dbb2c700 Error in foreign key constraint of table xxxxx/#sql-7b23_282b1a:

 FOREIGN KEY (`groupdeals_id`)
 REFERENCES `groupdeals` (`groupdeals_id`)
 ON DELETE CASCADE
 ON UPDATE CASCADE:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

The column groupdeals.groupdeals_id is a primary one, so I really can't understand where is a problem :|

Any hints?

Server type: Percona Server Server version: 5.6.11-rc60.3-log - Percona Server (GPL), Release 60.3

Was it helpful?

Solution

Your types are inconsistent:

CREATE TABLE IF NOT EXISTS `groupdeals` (
  `groupdeals_id` int(11) unsigned NOT NULL AUTO_INCREMENT,

CREATE TABLE IF NOT EXISTS `groupdeals_coupons` (
  [...]
  `groupdeals_id` int(11) NOT NULL,

As you will see, in one table you have int unsigned. In the other, just int.


BTW, I noticed you have two keys on groupdeals_id. Is that on purpose?

CREATE TABLE IF NOT EXISTS `groupdeals` (
  [...]
  PRIMARY KEY (`groupdeals_id`),  
  KEY `groupdeals_id` (`groupdeals_id`)

OTHER TIPS

Try these two steps to add foreign key:-

  1. alter table groupdeals_coupons modify groupdeals_id int unsigned not null default 0;

  2. ALTER TABLE groupdeals_coupons ADD CONSTRAINT fk_groupdeals_coupons_groupdeals1 FOREIGN KEY (groupdeals_id) references groupdeals (roupdeals_id);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top