Skip to content
Advertisement

The reference constraint is being triggered but unable to select rows from the table that would trigger it

I am attempting to update a bunch of different tables in my database. However, when the update is completed, I am unable to delete a record from another table due to a foreign key constraint. However, other databases on different servers will accept the change just fine. And upon creating a fresh new db and applying a series of update scripts against it to pre-populate the data, it fails as well.

It should be SQL Server 2016.

I have had a coworker attempt to rebuild indexes and update usages and it still failed.

I select out a collection of schema, columns and tables that meet my criteria and generate dynamic SQL for each combination.

Most/all of the tables returned by query 1 have a FK constraint against another table. And query 2 seems to execute correctly without issue.

Table definition is as follows:

My expectation is that when I run the following:

that I do not trigger FK_Table1_ColY_Table2_ColY.

Especially because:

returns no records.

The complete error message is as follows:

Advertisement

Answer

It seems that there was an issue fixed in an update to sql server.

Support Microsoft: Fix referential integrity constraints not being evaluated correctly.

The work around fixes detailed are:

  1. Use a compatibility level below 130.

  2. Modify the referencing table to change the index structure.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement