简体   繁体   中英

'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.

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. Institution Id is a foreign key in the roles table.

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. Is there a way I can get around this?

发现问题.... 我在做'@InstitutionId'而不仅仅是@InstitutionId .... FML ...

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