Forum Discussion
Unable to add CHECK CONSTRAINT
You have removed a row from one of the tables containing user information.
"dbo.dba_user"
"dbo.dba_user_attribute"
There's a "FOREIGN KEY" constraint that you removed. This constraint ensured that all entries in one table maps to entries in the other table.
Your 'ALTER TABLE [...] WITH CHECK CHECK CONSTRAINT [...]' fails because there's now a row in one of the tables that no longer map to the other.
This query should provide you with the offending row.
/*user attribute without user*/
SELECT dua.user_id FROM dbo.dba_user_attribute dua
EXCEPT
SELECT du.id FROM dbo.dba_user
GO
/*user withouth user attribute*/
SELECT du.id FROM dbo.dba_user
EXCEPT
SELECT dua.user_id FROM dbo.dba_user_attribute dua
You should delete the rows from the secondary table that no longer match your primary table, to ensure data consistency.
Or you can disregard data consistency entirely, and use 'ALTER TABLE [...] WITH NOCHECK CHECK CONSTRAINT [...]' instead. This is NOT recommended, as it can lead to other issues down the line.