Patrick
Patrick

Reputation: 2709

in Django, cannot delete object because view is not updatable

I have a model called Fiche which contains charfields and a couple of foreign keys. When I try to delete one row:

f = Fiche.objects.all()[0]
f.delete()

I get the following error:

('42000', "[42000] [FreeTDS][SQL Server]View or function 'entites_pilefusion_bFiche' is not updatable because the modification affects multiple base tables. (4405) (SQLExecDirectW)")

That view is partially based on Fiche okay but I should be able to delete one row of Fiche, no? Actually, when I try to delete the same row directly in SQL Server, I have no problem.

The problem is new, it seemed to appear after I add a new field in Fiche. I have no idea how to solve or even trace the problem. I'm completely lost , any help would be much appreciated. Patrick

EDIT: I solved the problem by adding in the model definition of PileFusionFiche (that is based on my view entites_pilefusion_bFiche) the clause on_delete=models.SET_NULL to avoid the ON DELETE CASCADE behavior on my view.

class PileFusionFiche(models.Model):

entite = models.ForeignKey('entites.Entite')
fiche  = models.ForeignKey('docentites.Fiche',blank=True,null=True,on_delete=models.SET_NULL)

class Meta:
    db_table = 'entites_pilefusion_bFiche'
    managed = False

Thanks again @Bistabil.

Upvotes: 0

Views: 511

Answers (1)

Bistabil
Bistabil

Reputation: 194

I'm guessing your problem appeared after you added a field from another table.

https://msdn.microsoft.com/en-us/library/ms187956.aspx

Paragraph about updatable views is what interests you.

You need a INSTEAD OF trigger to be able to affect multiple tables with a view.

Upvotes: 1

Related Questions