The DELETE statement conflicted with the REFERENCE constraint

rajesh prajapati 1 Reputation point
2021-07-28T03:45:14.353+00:00

I have a two tables static_data_value and calc_invoice_volume Pk-FK relationship with on delete no action defined during table creation.

value_id =invoice_line_item_id (PK - FK relationship )

The cal_invoice_volume has no row for the corresponding value_id =29 when try to delete the record from static_data_volume table ,got the following error:

The DELETE statement conflicted with the REFERENCE constraint "FK_calc_invoice_volume_static_data_value". The conflict occurred in database "TRMTracker_LADWP", table "dbo.calc_invoice_volume", column 'invoice_line_item_id'.

Command executed:

DELETE FROM static_data_value where value_id =29
Delete from calc_invoice_volume where invoice_line_item_id IN (29)

The PK table should have no issue to delete 29 row. Please let me know the solution for this issue.

Note: calc_invoice_volume table has other FK relationship with other tables here.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,808 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Seeya Xi-MSFT 16,446 Reputation points
    2021-07-28T08:13:04.723+00:00

    Hi @rajesh prajapati ,

    The error means that you have data in other tables that references the data you are trying to delete.
    This is a similar thread.
    You would need to drop and recreate the constraints.

    Best regards,
    Seeya


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar queries.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Erland Sommarskog 101.8K Reputation points MVP
    2021-07-28T12:42:43.37+00:00

    The cal_invoice_volume has no row for the corresponding value_id =29

    The resolution for this type of problem is to double-check your assumptions. There is a fair chance that the computer is right and you are wrong.

    DELETE FROM static_data_value where value_id =29
    Delete from calc_invoice_volume where invoice_line_item_id IN (29)

    Try running the command in reverse order.


  3. Erland Sommarskog 101.8K Reputation points MVP
    2021-07-29T11:43:01.577+00:00

    No, you don't need to remove the FK.

    Just to get this out of the way, run DBCC CHECKDB. It could be a case of corruption.

    It could also be that there is a cascading self-referencing FK on static_data_value, and this in its turn lead to the FK-violation. Yet a possibility is a trigger.