Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a Django model referenced by unmanaged view model

I have two models, one a vanilla Django model, another an unmanaged model wrapping a view.

class A(models.Model):
    name = models.CharField(max_length=255)

class B(models.Model):
    a = models.ForeignKey(A)

    class Meta:
        managed = False
        db_table = 'myview'

Everything works well, unless I try to delete a row from A, which gives me an error like:

django.db.utils.DatabaseError: cannot delete from view "myview"
HINT:  You need an unconditional ON DELETE DO INSTEAD rule or an INSTEAD OF DELETE trigger.

Apparently, Django is assuming the unmanaged model is a table, and is therefore trying to delete the dependent rows, and since it's a readonly view, my PostgreSQL backend is throwing this error.

Is there any way around this?

I found this bug report outlining the issue, but none of the stopgap hacks seem to work for me, and the bug itself was marked a duplicate of another slightly different yet unresolved bug...

like image 389
Cerin Avatar asked Oct 24 '25 05:10

Cerin


1 Answers

have you tried on_delete=models.SET_NULL as a workaround?

like image 52
second Avatar answered Oct 25 '25 20:10

second



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!