Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django on delete cascade not producing the right constraint in PostgreSQL

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?

like image 602
gozzilli Avatar asked Mar 21 '23 12:03

gozzilli


1 Answers

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

like image 159
Xealot Avatar answered Apr 19 '23 23:04

Xealot