我有两个InnoDB表。第一个表有一个2字段主键,两个字段都使用参照完整性进行验证,并且一切正常。第二个表使用相同的两个字段验证第一个表:
表1:
CREATE TABLE valid_combo(
season_id CHAR(16) NOT NULL,
ad_id INT UNSIGNED NOT NULL,
PRIMARY KEY (season_id, ad_id)
)ENGINE=INNODB
COMMENT='Parent Table of valid choices'
;
创建记录
INSERT INTO valid_combo (season_id, ad_id) VALUES ('SEASON', 100);
表2:
CREATE TABLE user_combo(
uid INT UNSIGNED NOT NULL,
season_id CHAR(3) NOT NULL,
ad_id INT UNSIGNED NOT NULL,
PRIMARY KEY (uid, season_id, ad_id),
INDEX IDX_season_ad(season_id, ad_id),
UNIQUE INDEX AK_seasons_users_ads(season_id, uid, ad_id),
CONSTRAINT Refseason_ads451 FOREIGN KEY (season_id, ad_id)
REFERENCES valid_combo(season_id, ad_id)
)ENGINE=INNODB
COMMENT='Child table with users choices out of valid combos'
;
不知何故,我无法添加应该验证的记录。
INSERT INTO user_combo (uid, season_id, ad_id) VALUES (200, 'SEASON', 100);
我收到错误:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`user_combo`, CONSTRAINT `Refseason_ads451` FOREIGN KEY (`season_id`, `ad_id`) REFERENCES `valid_combo` (`season_id`, `ad_id`))
我知道并看到父表中存在引用的值。我怀疑多个字段主键和参照完整性是罪魁祸首。但我可能错了,没有看到明显的东西。
我没有使用单个字段进行验证的原因是因为组合必须有效且存在。
有什么想法吗?
答案 0 :(得分:1)
字段season_id的大小不一样......
CHAR(16) and CHAR(3).
我不知道如何关闭这个话题。