In Django, I have a model:
class HmmInsectValue(models.Model):
hmm = models.ForeignKey('HmmResult', on_delete=models.CASCADE)
...
on a PostgreSQL database (9.3.2) this produces a constraint:
FOREIGN KEY (hmm_id) REFERENCES devdash_hmmresult(id) DEFERRABLE INITIALLY DEFERRED
while what I really want is:
FOREIGN KEY (hmm_id) REFERENCES devdash_hmmresult(id) ON DELETE CASCADE
This breaks my code because when I try to delete an HmmResult
entry it complains that it's is still referenced from HmmInsectValue
. If I replace it manually with the line above it all works fine.
Any idea why this is happening?
Because Django simulates the on_delete clause in the application tier. I believe it does this so it can still provide things like delete signals and the like.
The on_delete
attribute of ForeignKey
instructs Django what to do, it does not translate directly into the database construction statements.
See Also: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.DO_NOTHING
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With