MAX POWER
MAX POWER

Reputation: 5438

Foreign Key Constraint Problem

The tables in question are:

enquiry

supplier_enquiry

quote

The constraints are as follows:

So the way I expect this to work is:

  1. If you delete an 'enquiry' it deletes the child 'supplier_enquiry' records
  2. If you delete a 'supplier_enquiry' it deletes the child 'quote' records
  3. You cannnot delete a 'quote' if an 'enquiry' references that quote's ID

The problem I'm having is when deleting an 'enquiry' record. Because it needs to delete the child records first, i.e. 'supplier_enquiry' and 'quote', if 'accepted_quote_id' references a 'quote' then the 'enquiry' can't be deleted.

Any idea how I can overcome this issue?

Upvotes: 0

Views: 217

Answers (2)

Parris Varney
Parris Varney

Reputation: 11478

You get rid of the circular reference by making the two other tables dependant on enquiry.

So:

quote.enquiry_id references enquiry.id

And:

supplier_enquiry.enquiry_id references enquiry.id

EDIT: That might be unclear. I'm suggesting making a new constraint on the quote table that references enquiry_id to enquiry.id, then removing enquiry_ibfk_9

Upvotes: 1

Core Xii
Core Xii

Reputation: 6441

Update the reference to NULL before deleting.

Upvotes: 0

Related Questions