Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't my GenericForeignKey cascade when deleting?

Tags:

I'm creating a custom commenting system which can attache comments to any model using the contenttypes GenericForeignKey.

class Comment(models.Model):     body = models.TextField(verbose_name='Comment')     user = models.ForeignKey(User)     parent = models.ForeignKey('self', null=True, blank=True)     created = models.DateTimeField(auto_now_add=True)     content_type = models.ForeignKey(ContentType)     object_id = models.PositiveIntegerField()     content_object = generic.GenericForeignKey('content_type', 'object_id') 

It is my understanding that when the model the comment is attached to is deleted, the delete should cascade and remove the comment as well.

Unfortunately, this isn't happening and I'm stumped. Are there any common reasons why the default delete behaviour would change?

like image 916
Soviut Avatar asked Jul 23 '11 20:07

Soviut


2 Answers

No, the documentation doesn't say that. What it says is that if you define a GenericRelation on a model - ie the reverse side of the GenericForeignKey - then when the item with the generic FK is deleted, the item with the GenericRelation will also be deleted.

Unlike ForeignKey, GenericForeignKey does not accept an on_delete argument to customize this behavior; if desired, you can avoid the cascade-deletion simply by not using GenericRelation, and alternate behavior can be provided via the pre_delete signal.

like image 120
Daniel Roseman Avatar answered Sep 28 '22 16:09

Daniel Roseman


I realise this is a very old question so it's possible things are different from when this was asked, but the accepted answer had me chasing down a rabbit hole this morning, therefore I wanted to leave this here to prevent future generations sharing my pain.

From the docs:

Note also, that if you delete an object that has a GenericRelation, any objects which have a GenericForeignKey pointing at it will be deleted as well. In the example above, this means that if a Bookmark object were deleted, any TaggedItem objects pointing at it would be deleted at the same time.

This is the opposite of what the accepted answer is saying. Imagine the following:

class Comment(models.Model):     body = models.TextField(verbose_name='Comment')     user = models.ForeignKey(User, on_delete=models.CASCADE)     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)     object_id = models.PositiveIntegerField()     content_object = generic.GenericForeignKey('content_type', 'object_id')  class Post(models.Model):     comment = GenericRelation(Comment) 

In the above example, if your Comment object has a Generic Foreign Key pointing to a Post object, then when the Post object is deleted any Comment objects pointing to it will also be deleted.

This is the expected behaviour and operates the same as a normal ForeignKey. Using the same example above, if the User object that the Comment object points to is deleted, the Comment will also be deleted.

If you happen to chance across this question because you need the reverse of this behaviour, i.e. when you delete the Comment the Post is also deleted, then you will likely need to employ the power of signals.

like image 40
acardnell24 Avatar answered Sep 28 '22 16:09

acardnell24