How to fix 'Cannot add or update a child row: a foreign key constraint fails' in MySql after deployment to linux server?

0

I've got MySql database and ASP.NET Core 2.2 app working okay on my local Windows machine. But when I publish them to Ubuntu 18.10 and try to insert any child record for my User, I get the following exception:

MySql.Data.MySqlClient.MySqlException (0x80004005): Cannot add or update a child row: a foreign key constraint fils (library.Books, CONSTRAINT FK_Books_Users_UserId FOREIGN KEY (UserId) REFERENCES users (id))

Before creating a book, the user is already created and has Id.

Both tables use InnoDB engine.

Both tables have identical collation.

This problem happens for all the models the User has connections with.

The deployed database was populated with my local database dump.

Database Provider: Pomelo.EntityFrameworkCore.MySql

I think the problem must be in some difference between Windows and Ubuntu. But I don't know what it can be except different case sensitivity, which turns out is not a problem here as I tried different lower_case_table_names configurations.

mysql
linux
ef-code-first
entity-framework-core
ef-core-2.2
asked on Stack Overflow Apr 26, 2019 by upkit • edited Apr 26, 2019 by upkit

1 Answer

0

The problem is with mysqldump. As mentioned in the question, the local db was exported and then imported into production environment. But local and production MySQL versions were different. Some table and column names were exported in lowercase.

The solution was to rename all the broken names to the valid ones and only then import the dump. Or if you do not want to so each time, dump your local database with mysqldump version which corresponds to the on on the production database.

answered on Stack Overflow Apr 27, 2019 by upkit • edited Aug 15, 2019 by upkit

User contributions licensed under CC BY-SA 3.0