简体   繁体   English

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

[英]'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.

相关问题 错误-无法添加或更新子行:外键约束失败 - Error - Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Cannot add or update a child row: a foreign key constraint fails EF6代码优先:MySqlException:无法添加或更新子行:外键约束失败 - EF6 Code First: MySqlException: Cannot add or update a child row: a foreign key constraint fails ASP.NET:无法添加或更新子行:外键约束失败 - ASP.NET: Cannot add or update child row: a foreign key constraint fails ASP.NET MVC 5 EF无法添加或更新子行:外键约束失败 - ASP.NET MVC 5 EF Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:在 foreach 中执行 INSERT 时外键约束失败 - Cannot add or update a child row: a foreign key constraint fails while doing INSERT in a foreach C# - 无法添加或更新子行:外键约束失败 - C# - Cannot add or update a child row: a foreign key constraint fails 无法删除或更新父行:外键约束失败 - Cannot delete or update a parent row: a foreign key constraint fails C#:在数据库中添加对象时,出现“ MySqlException:无法添加或更新子行:外键约束失败” - C#: I'm getting a “MySqlException: Cannot add or update a child row: a foreign key constraint fails” when i am Adding an object in my database C#FrameworkCore和MySQL:无法删除或更新父行:外键约束失败 - C# FrameworkCore and MySQL: Cannot delete or update a parent row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM