|
-
May 11th, 2018, 05:02 AM
#1
The DELETE statement conflicted with the REFERENCE constraint... - which record?
I'm pretty sure what I want can't be done but I really want it so I'm posting in blind hope
I'm building a purge process to support GDPR for a client. This involves (among other things) copying aged data into a locked down database and then deleting it from the main production database. The data is spread across multiple tables which have Foreign Key constraints in place.
My problem is when I come to do the delete. I delete aged data from a child table then from the parent table. Occasionally, a new insert into the child could interleave creating a new child record against the, yet to be deleted, parent. This will cause the Parent deletion to fail due to the Foreign Key constraint. This isn't a major problem as the whole thing can be investigated and, if necessary, recovered into the production database from the locked down database. I do, however, need to report it happening to the DBAs so that they know there's something that needs to be investigated.
I'd like to be able to report the Id (or whatever the primary key value is) of the offending parent if I can. But the error message doesn't include that information (in full it reads [The DELETE statement conflicted with the REFERENCE constraint "fk_Child_Parent". The conflict occurred in database "Purge", table "dbo.Child", column 'ParentId'.])
The DBAs could, of course, throw together a query to find any record that would violate the constraint if it were deleted but I'd rather save them the work. It just sorta feels like I should be able to recover it at the point of error. Any suggestions on how I can identify the culprit?
Just to head some possible suggestions (which I've already considered) off at the pass:-
1. This DB is in HEAVY use, is web facing and the deletes are big enough to take a significant amount of time. Wrapping things up in a transaction isn't an option because we can't block activity on the web page.
2. Cascading deletes (or anything similar) isn't an option because the interleaved child record wouldn't have been copied to the locked down DB so wouldn't be recoverable.
So really, I just want that ID. Any thoughts?
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|