Home Django 1.11 ignoring on_delete=models.set_null
Reply: 0

Django 1.11 ignoring on_delete=models.set_null

user1284
1#
user1284 Published in April 23, 2018, 8:54 pm

I have a Django project with a model, something like this:

class Track(models.Model):
    title = models.CharField(max_length=255)
    name = models.CharField(max_lenght=255)


class Disc(models.Model):
    a_side_disc = models.ForeignKey(Track, null=True,
                                    on_delete=models.SET_NULL)
    b_side_disc = models.ForeignKey(Track, null=True,
                                    on_delete=models.SET_NULL)
    in_jukebox = models.BooleanField()

This model is totally made up, and don't make sense, but it shows my current setup in this case.

Now with Django 1.11 and Django Restframework, I have a route with a delete for a Track. When I call the Route, I check if the Track is on a Disc with in_jukebox set to False. If that is the case I simply go on with the delete. I expect that both ForeignKeys in Disc are set to Null, but instead Disc is deleted as well like I would expect from CASCADE.

I use Postgres as database, and inspected the generated Schema. I understand that SET_NULL is done by django and not the database. But it is not done. Am I understanding somthing wrong? Which direction is on_delete working, and if it's only one, how do I make the same on the other side?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301678 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO