1

我在创建外键时遇到了这个奇怪的问题。

给定2张桌子:

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;

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;

我尝试通过执行以下查询来添加外键:

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

我收到的只是错误:

#1215 - Cannot add foreign key constraint

显示引擎 innodb 状态

------------------------
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.

groupdeals.groupdeals_id 列是主要列,所以我真的不明白哪里有问题:|

有什么提示吗?

服务器类型:Percona Server 服务器版本:5.6.11-rc60.3-log - Percona Server (GPL),Release 60.3

4

2 回答 2

6

您的类型不一致:

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,

如您所见,在一张表中,您有int unsigned. 另一方面,只是int


顺便说一句,我注意到你有两个groupdeals_id。这是故意的吗?

CREATE TABLE IF NOT EXISTS `groupdeals` (
  [...]
  PRIMARY KEY (`groupdeals_id`),  
  KEY `groupdeals_id` (`groupdeals_id`)
于 2013-08-17T10:55:34.360 回答
0

尝试以下两个步骤来添加外键:-

  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);

于 2013-08-17T10:54:18.877 回答