执行插入时出现MySQL错误1452

时间:2012-10-23 08:32:50

标签: mysql

这个问题可能真的很愚蠢,我知道它之前已被问过,但我在以前的帖子中找不到任何帮助我的内容。我有2个表(实际上还有很多,但有2个相关):

create table user (
email varchar(100) primary key,
first_name varchar(20) not null,
last_name varchar(20) not null,
password_hash varchar(100) not null,
salt varchar(10) not null,
account_status enum('pending','active','locked','retired') not null,
user_type enum('partsrequestor', 'manager', 'sysadmin') not null,
security_question varchar(256) not null,
security_question_answer varchar(64) not null,
constraint email_user_type_unique unique (email, user_type)
);

create table sysadmin (
email varchar(100) primary key,
user_type enum('sysadmin') not null,
foreign key (email, user_type) references user (email, user_type)
);

我正在尝试将一些示例数据添加到表中:

insert into user values
('usera@company.com','firstnamea','lastnamea','c448602d3063e83d486403cc7566706d4eb40c8cb674d6cfb5436d078bc574dc','0590','active','partsrequestor','security question a', 'Answer a'), 
('userb@company.com','firstnameb','lastnameb','44367f83812a4478631e5d02a2b833ebe0a17e405a1c4a5700e41891d6b4ecb6','0591','pending','partsrequestor','security question b', 'Answer b'),
('userc@company.com','firstnamec','lastnamec','028973863316e14501338d012222f4b425fd91d3a50639c51a2aaab489d4dd2c','0592','locked','partsrequestor','security question c', 'Answer c'),
('userd@company.com','firstnamed','lastnamed','05ed7e29083998f403eb5531986687a87aafa3d954e59119389e8779fbc08f2e','0593','retired','partsrequestor','security question d', 'Answer d'),
('usere@company.com','firstnamee','lastnamee','e0e0f0162016e0baccf6cfa19dd184c1b39c02c12bec4aee81755db1cd1e4b6d','0594','active','manager','security question e', 'answer e'),
('userf@company.com','firstnamef','lastnamef','ba8e9abdc4e7640cdfc672b63d86f8aca72c91b729b5d462182482a0dc59d38b','0515','active','sysadmin','security question f', 'answer f')
;

insert into sysadmin values
('userf@company.com', 'sysadmin')
;

sysadmin中的插入失败并带有

  

错误1452(“无法添加或更新子行:外键约束失败”)

我尝试禁用外键检查,运行插入,并在两个表之间的电子邮件和user_type上进行连接; userf出现就好了,所以我知道数据不匹配。另一张海报还提到使用不同的存储引擎在使用不同的表时遇到问题,因此我从头开始重新创建了整个数据库并在添加表之前运行了set storage_engine = innodb;,但它没有帮助。

有什么想法吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

我认为ENUM类型不兼容,尝试以这种方式更改user_type字段 -

create table user (
...
user_type enum('sysadmin', 'partsrequestor', 'manager') not null,
...
);

...以便'sysadmin'位于第一位,就像在sysadmin表中一样。

答案 1 :(得分:0)

尝试更改表格以使其与命令SET FOREIGN_KEY_CHECKS = 0;一起使用并尝试再次运行查询?