Monday, September 26, 2016

Re: Deleting PROTECTED objects that would be deleted by a CASCADE anyway

And now a Trac ticket for this: https://code.djangoproject.com/ticket/27272

On Friday, September 23, 2016 at 7:17:32 AM UTC-4, Daniel Izquierdo wrote:
Hello,

Consider this set of models:

class Artist(models.Model):
    name
= models.CharField(max_length=10)

class Album(models.Model):
    artist
= models.ForeignKey(Artist, on_delete=models.CASCADE)                                                                                                                                                            
class Song(models.Model):
    artist
= models.ForeignKey(Artist, on_delete=models.CASCADE)
    album
= models.ForeignKey(Album, on_delete=models.PROTECT)


And the following test:

class TestDeletion(TestCase):
   
def test_delete(self):
        artist
= Artist.objects.create(name='test')
        album
= Album.objects.create(artist=artist)
       
Song.objects.create(artist=artist, album=album)

        artist
.delete()

What is the expected result of the test?

Currently, this test fails with ProtectedError raised, because trying to delete the artist results in trying to delete the album, which is protected because of the related song. But, the related song was going to be deleted anyway, via the cascading relationship to the artist itself. So I believe that in this case, deletion should succeed.

If there's agreement that the current behavior is a bug, I'll open a ticket with a proper test case and work on a fix.

Thanks,

--
Daniel Izquierdo

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/be34718c-73e1-469d-a81c-1a7e9be79957%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment