插入具有外部约束的UUID列失败

时间:2015-06-25 01:20:59

标签: mysql sql phpmyadmin foreign-key-relationship composite-primary-key

我有以下表结构&数据:

votes

season | episode | UUID    | vote
1      | 1       | X-X-X-X | 1
1      | 1       | Y-Y-Y-Y | -1

users

UUID    | name
X-X-X-X | Jon
Y-Y-Y-Y | Sam
Z-Z-Z-Z | Tim

votes表中,运行后

ALTER TABLE votes ADD PRIMARY KEY (season, episode, UUID);
ALTER TABLE votes ADD INDEX (UUID);
ALTER TABLE votes ADD FOREIGN KEY (UUID) REFERENCES user (UUID)
                           ON DELETE CASCADE ON UPDATE CASCADE;

并尝试插入这样的条目:

season, episode, UUID,      vote
1,      1,       'Z-Z-Z-Z', 1

进入上表,以下错误问候我:

  

无法添加或更新子行:外键约束失败(投票,CONSTRAINT`contact_name` FOREIGN KEY(UUID)REFERENCES用户(UUID)ON DELETE CASCADE ON UPDATE CASCADE)

我想保持UUID链接到users表,同时不允许每个季节多次投票 - 剧集 - UUID组合,我该如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

问题是我使用phpMyAdmin的Insert选项卡添加数据,它从Functions下拉列表中自动选择了UUID,这改变了我想要插入的数据。将下拉列表设置为空值会使我的插入值单独保留,并且添加得很好。