Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rretzko's avatar
Level 15

Best Practice: Soft-Deleting Parent with Soft-Deleted Child Models

Hi - I've hit the "Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails" when attempting to soft-delete the parent of the soft-deleted child. I put this issue through Larry AI and the suggested alternatives are:

  1. Remove the soft-delete property of the child models.
  2. Remove the model linkage between parent/child and leave the child models intact when soft-deleting the parent model, effectively leaving 'live' models on the database but orphaned.
  3. Hard delete everything. The model I'm working with has six child tables and the likelihood of the user mistakenly deleting or misunderstanding the impact of the deletion is probably 33%, so having soft-delete protection is important, but not mission-critical. I've put an 'Are you sure?" alert on the button so that there's an opportunity to cancel the action. My question: In your experience, What are the best practices for handling this? Thanks - Rick
0 likes
5 replies
krisi_gjika's avatar

"when attempting to soft-delete the parent" - are you sure? A soft-delete attempt should not trigger a DB error.

1 like
tykus's avatar

This should not be the case; can you share the code you have written which triggers this error?

Rretzko's avatar
Level 15

@krisi_gjika @tykus Thanks for replying! I followed the instructions from LarryAI using the onDelete('cascade') options in my migrations and the system is now working as expected. Thanks for the offers of assistance.

Please or to participate in this conversation.