0

I just implemented a comment feature into my blog app. The issue I am facing is if I try to delete a blog post that has comments I get the postgres error enter image description here

If I understand this error correctly I cant delete the post because the comment is referencing the post id. To solve this issue I figured I would just add on_delete=models.CASCADE to the model. Unfortunately this did not resolve the issue. I was under the impression that with the CASCADE it would delete the entire model if the FK was deleted.

the two models.

class Post(models.Model):
    title = models.CharField(max_length=100)
    image = models.ImageField(default='default.jpg', upload_to='hero_car_image')
    aspiration = models.CharField(max_length=55, choices=ASPIRATION)
    content = RichTextUploadingField(blank=True, null=True)
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    manufacture = models.ForeignKey(Manufactures, on_delete=models.CASCADE)
    model = models.ForeignKey(Models, on_delete=models.CASCADE)

class Comment(models.Model):
    comment = models.TextField()
    created_on = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey('Post', on_delete=models.CASCADE)
Wazy
  • 462
  • 4
  • 17

0 Answers0