0

这个问题可能真的很愚蠢,我知道以前有人问过这个问题,但是我在以前的帖子中找不到任何对我有帮助的东西。我有 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')
;

插入系统管理员失败

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

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

有任何想法吗?

提前致谢

4

2 回答 2

2

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

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

...所以“系统管理员”排在第一位,就像在sysadmin表中一样。

于 2012-10-23T08:53:56.497 回答
0

尝试更改您的表以使其与命令一起工作:SET FOREIGN_KEY_CHECKS = 0;并尝试再次运行查询?

于 2012-10-23T08:37:20.447 回答