需要有关不可思议的、可重现的 MySQL ERROR 1452 (23000) 外键约束的帮助

发布于 2024-10-30 02:00:39 字数 2347 浏览 6 评论 0原文

MySQL select version() is 5.5.9 on Mac OS/X 10.6

问题

当我执行下面的sql脚本时,我遇到了一个非常令人困惑的外键约束错误。似乎它不应该抛出这个错误。此外,我知道其他人已尝试按照这些步骤操作,但无法重现(请参阅:http://forums.mysql.com/read.php?10,415350,415350#msg-415350

任何人都可以发现我们做错了什么吗?

重现:

  1. 创建数据库constraint_test;
  2. 创建constraint_test.sql 文件并粘贴下面的sql。
  3. 从cmd行执行“mysql constrain_test

预期结果:行保存到客户端、受保人和受益人表中。

实际结果:如您所见,我们始终收到的外键约束错误类似于:

“第 55 行错误 1452 (23000):无法添加或更新子行:外键约束失败 ( constraint_test.受益人,约束 FK41BADEC55CE3480 外键 (insured_id) 参考 被保险人 (< code>insured_id))"

但是,父表 Insured 中肯定有一个 insured_id,

如果可以请帮忙!

constraint_test.sql 内容:

create table Beneficiary ( 
beneficiary_id bigint not null, 
district varchar(255), 
serviceUnit varchar(255), 
insuredNo integer, 
beneficiaryIndex integer, 
relationship varchar(255), 
percentage double precision, 
fullName varchar(255), 
lastUpdatedDate datetime, 
insured_id bigint, 
contractNo varchar(255), 
primary key (beneficiary_id) 
); 

create table Client ( 
client_id bigint not null, 
firstName varchar(255), 
lastName varchar(255), 
email varchar(255), 
initial varchar(255), 
birthDate datetime, 
district varchar(255), 
serviceUnit varchar(255), 
genderType varchar(255), 
externalId varchar(255), 
externalTempId varchar(255), 
taxationProvince varchar(255), 
children varchar(255), 
manufacturerClientNumber varchar(255), 
primary key (client_id) 
); 

create table Insured ( 
insured_id bigint not null, 
client_id bigint not null, 
insuredNo integer, 
primary key (insured_id) 
); 

alter table Beneficiary 
add index FK41BADEC55CE3480 (insured_id), 
add constraint FK41BADEC55CE3480 
foreign key (insured_id) 
references Insured (insured_id); 

alter table Insured 
add index FKD7E770CAC207FE14 (client_id), 
add constraint FKD7E770CAC207FE14 
foreign key (client_id) 
references Client (client_id); 

insert into Client (client_id) values (1); 
insert into Insured (insured_id, client_id ) values (1,1); 
insert into Beneficiary (beneficiary_id, insured_id) values (1,1);

MySQL select version() is 5.5.9 on Mac OS/X 10.6

Question

When I execute the sql script below, I encounter a very perplexing foreign key constraint error. It seems as though it should not throw this error. Moreover, I know that others have attempted to follow the steps but are unable to repro (see: http://forums.mysql.com/read.php?10,415350,415350#msg-415350)

Can anyone spot what it is we're doing wrong?

To reproduce:

  1. create database constraint_test;
  2. create the constraint_test.sql file and paste the sql below.
  3. from the cmd line execute "mysql constraint_test < constraint_test.sql" (or in my sql execute "source tmp/constraint_test.sql"

Expected result: row persisted to client, insured and beneficiary tables.

Actual result: As you'll see, the foreign key constraint error we consistently receive is similar to:

"ERROR 1452 (23000) at line 55: Cannot add or update a child row: a foreign key constraint fails (constraint_test.beneficiary, CONSTRAINT FK41BADEC55CE3480 FOREIGN KEY (insured_id) REFERENCES Insured (insured_id))"

However, there is definitely an insured_id in the parent table Insured.

Please help if you can!

constraint_test.sql contents:

create table Beneficiary ( 
beneficiary_id bigint not null, 
district varchar(255), 
serviceUnit varchar(255), 
insuredNo integer, 
beneficiaryIndex integer, 
relationship varchar(255), 
percentage double precision, 
fullName varchar(255), 
lastUpdatedDate datetime, 
insured_id bigint, 
contractNo varchar(255), 
primary key (beneficiary_id) 
); 

create table Client ( 
client_id bigint not null, 
firstName varchar(255), 
lastName varchar(255), 
email varchar(255), 
initial varchar(255), 
birthDate datetime, 
district varchar(255), 
serviceUnit varchar(255), 
genderType varchar(255), 
externalId varchar(255), 
externalTempId varchar(255), 
taxationProvince varchar(255), 
children varchar(255), 
manufacturerClientNumber varchar(255), 
primary key (client_id) 
); 

create table Insured ( 
insured_id bigint not null, 
client_id bigint not null, 
insuredNo integer, 
primary key (insured_id) 
); 

alter table Beneficiary 
add index FK41BADEC55CE3480 (insured_id), 
add constraint FK41BADEC55CE3480 
foreign key (insured_id) 
references Insured (insured_id); 

alter table Insured 
add index FKD7E770CAC207FE14 (client_id), 
add constraint FKD7E770CAC207FE14 
foreign key (client_id) 
references Client (client_id); 

insert into Client (client_id) values (1); 
insert into Insured (insured_id, client_id ) values (1,1); 
insert into Beneficiary (beneficiary_id, insured_id) values (1,1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2024-11-06 02:00:39

您的数据类型不完全匹配:
Beneficiary.insured_id 是 bigint,而 Insured.insured_id bigint not null

Innodb 对这些非常敏感,请确保两个表中的 FK 列定义完全相同,包括可为空性。

Your datatypes don't match exactly:
Beneficiary.insured_id is bigint, whilst Insured.insured_id bigint not null

Innodb is very very touchy about these, make sure that your FKs columns definitions are exactly the same in both tables, including nullability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文