简体   繁体   中英

MySQL error 1452 when doing insert

This question is probably really stupid and I know it has been asked before, but I couldn't find anything in the previous posts that helped me. I have 2 tables (actually there are a lot more but there are 2 that are relevant here):

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

I'm trying to add some sample data into the tables:

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

The insert into sysadmin fails with

error 1452 ("Cannot add or update a child row: a foreign key constraint fails")

I tried disabling foreign key checking, ran the insert, and did a join on email and user_type between the two tables; userf showed up just fine, so I know the data isn't mismatched. Another poster also mentioned having trouble with different tables using different storage engines, so I re-created the entire database from scratch and ran a set storage_engine = innodb; before adding the tables back, but it did not help.

Any ideas?

Thanks in advance

I think that ENUM types are incompatible, try to change user_type field in this way -

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

...so that 'sysadmin' was on first place, like in sysadmin table.

Try altering your table to make it work with the command: SET FOREIGN_KEY_CHECKS = 0; and try running the query again?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM