shlogg · Early preview
Valerio @ilvalerione

How To Fix Integrity Constraint Violation Error In SQL Database

Fixing "Integrity constraint violation" error in SQL databases: Declare new column as nullable or use data migration job to fill valid IDs before adding foreign key constraint.

If you are dealing with the error: "Integrity constraint violation: Cannot add or update a child row: a foreign key constraint fails", you are in the right article.

For more technical articles you can follow me on Linkedin or X. I post about building my SaaS product.

Usually you encounter this error when you add a new column to a table and declare it as a Foreign Key. 
In a SQL database, a foreign key is a field in a table used to establish a link between the data in another table. Consider the customers table below.

CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(255),...