[英]'Cannot add or update a child row: a foreign key constraint fails' Error
So in my institutions table, I already have an institution Id prepopulated that I have stored in my C# code.因此,在我的机构表中,我已经预先填充了一个机构 ID,并将其存储在我的 C# 代码中。
I am attempting to add a role to a role table, where I equally need to insert the institution Id I've already captured and stored in C# from the institutions table.我正在尝试向角色表中添加一个角色,我同样需要在其中插入我已经从机构表中捕获并存储在 C# 中的机构 ID。 Institution Id is a foreign key in the roles table.
机构 ID 是角色表中的外键。
Problem is, I do this:问题是,我这样做:
MySqlCommand mysqlcom = new MySqlCommand("INSERT INTO roles(RoleId, RoleType, InstitutionId) VALUES('(uuid_to_bin(uuid()))', " +
"'@RoleType', '@InstitutionId')",
mysqlcon);
mysqlcom.Parameters.Add("@RoleType", MySqlDbType.VarChar).Value = roleType;
mysqlcom.Parameters.Add("@InstitutionId", MySqlDbType.Binary).Value = InstitutionModel.InstitutionId;
mysqlcon.Open();
int isSuccess = mysqlcom.ExecuteNonQuery();
And get this:得到这个:
MySql.Data.MySqlClient.MySqlException: 'Cannot add or update a child row: a foreign key constraint fails (`TestDB`.`roles`, CONSTRAINT `InstitutionId_Roles_FK` FOREIGN KEY (`InstitutionId`) REFERENCES `institutions` (`InstitutionId`) ON DELETE CASCADE ON UPDATE CASCADE)'
The InstitutionId
is a BINARY 16 (UUID), and I have it stored as a byte[]
appropriately, and it is definitely the correct id, so i don't understand why MySQL isn't letting me do this. InstitutionId
是一个 BINARY 16 (UUID),我将它适当地存储为一个byte[]
,它绝对是正确的 ID,所以我不明白为什么 MySQL 不让我这样做。 Is there a way I can get around this?有没有办法解决这个问题?
发现问题.... 我在做'@InstitutionId'
而不仅仅是@InstitutionId
.... FML ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.