Integrity-error django :violates not-null constraint

I build a social media application where all actions are concluded in the same count. However with the like button i get an error even if the code looks exactly the same as the dislike version. The error is:

IntegrityError at /like-post
null value in column "num_interactions_total" of relation "core_likepost" violates not-null constraint
DETAIL:  Failing row contains (98, a4aaa95b-a3a5-49a6-8f63-de768bfa0786, 2023-02-02 18:01:13.121023+00, , 1, null, 7).

but the num_interactions_total count is already deleted and does not exist in the model or views.py anymore. I've also deleted the migrations. I'm using the postgresql database.

I´m a little confused, why it gives me an error only with this button and not with all the others... How do I avoid the null error?

Here are the models and views from like and dislike:

def like_pdf(instance, filename):
    ext = filename.split('.')[-1]
    filename = 'post{}.{}'.format(instance.interactions_total, ext)
    return os.path.join('uploads', filename)


class LikePost(models.Model):
    post_id = models.CharField(max_length=500)
    created_at = models.DateTimeField(default=datetime.now)
    num_likes_total = models.IntegerField(default=0)
    interactions_total = models.IntegerField(default=0)
    pdf_like = models.FileField(upload_to=like_pdf, default=None, null=True)
    def __str__(self):
        return self.post_id


def dislike_pdf(instance, filename):
    ext = filename.split('.')[-1]
    filename = 'post{}.{}'.format(instance.interactions_total, ext)
    return os.path.join('uploads', filename)

class DislikePost(models.Model):
    post_id = models.CharField(max_length=500)
    created_at = models.DateTimeField(default=datetime.now)
    num_dislikes_total = models.IntegerField(default=0)
    interactions_total = models.IntegerField(default=0)
    pdf_dislike = models.FileField(upload_to=dislike_pdf, default=None, null=True)


    def __str__(self):
        return self.post_id




def like_post(request):
    post_id = request.GET.get('post_id')
    post = Post.objects.get(id=post_id)
    post.number_of_likes = post.number_of_likes + 1
    num_likes_total = LikePost.objects.count() + 1
    interactions_total = InteractionPost.objects.count()
    new_like = LikePost.objects.create(post_id=post_id, num_likes_total=num_likes_total, interactions_total=interactions_total)
    new_like.save()
    new_interaction = InteractionPost.objects.create(post_id=post_id)
    new_interaction.save()
    post.interaction_count = post.interaction_count + 1



    likes_string = ""
    word = "like "

    i = 0
    while i < 1:
        post.likes_string = post.likes_string + word + os.linesep
        i += 1

    post.save()

    # create pdf dislike
    buffer = io.BytesIO()
    countobject = UserCount.objects.first()


    p = canvas.Canvas(buffer, pagesize=A4)

    x_start = 50
    y_start = 200
    x = 100
    y = 275

    p.roundRect(x_start, y_start, 500, 400, 10, stroke=1, fill=0)
    p.roundRect(x, y, 400, 300, 10, stroke=1, fill=0)

    p.drawString(100, 700, "new_interaction: like_post // user no.:")
    p.drawString(100, 675, str(new_like.created_at))
    p.drawString(200, 650, str(post.num_post))
    p.drawString(100, 650, "post no.:")
    p.drawString(400, 700, str(countobject))

    p1 = Paragraph(post.dislikes_string)
    p1.wrapOn(p, 400, 1000)
    p1.drawOn(p, 200, 700)

    p2 = Paragraph(post.likes_string)
    p2.wrapOn(p, 400, 1000)
    p2.drawOn(p, 0, 800)
    p.save()

    buffer.seek(0)

    new_like.pdf_like.save(f"post{interactions_total}.pdf", buffer)
    new_like.save()

    return redirect('/')


def dislike_post(request):
    post_id = request.GET.get('post_id')
    post = Post.objects.get(id=post_id)
    post.number_of_dislikes = post.number_of_dislikes + 1
    num_dislikes_total = DislikePost.objects.count() + 1
    interactions_total = InteractionPost.objects.count()

    new_dislike = DislikePost.objects.create(post_id=post_id, num_dislikes_total=num_dislikes_total, interactions_total=interactions_total)
    new_dislike.save()
    new_interaction = InteractionPost.objects.create(post_id=post_id)
    new_interaction.save()
    post.interaction_count = post.interaction_count + 1

    dislikes_string = ""
    word = "dislike "
    i = 0
    while i < 1:
        post.dislikes_string = post.dislikes_string + word + os.linesep
        i += 1

    post.save()

    #create pdf dislike
    buffer = io.BytesIO()
    countobject = UserCount.objects.first()

    p = canvas.Canvas(buffer, pagesize=A4, )

    x_start = 50
    y_start = 200
    x = 100
    y = 275

    p.roundRect(x_start, y_start, 500, 400, 10, stroke=1, fill=0)
    p.roundRect(x, y, 400, 300, 10, stroke=1, fill=0)

    p.drawString(100, 700, "new_interaction: dislike_post // user no.:")
    p.drawString(100, 675, str(new_dislike.created_at))
    p.drawString(200, 650, str(post.num_post))
    p.drawString(100, 650, "post no.:")
    p.drawString(400, 700, str(countobject))

    p1 = Paragraph(post.dislikes_string)
    p1.wrapOn(p, 400, 1000)
    p1.drawOn(p, 0, 300)

    p2 = Paragraph(post.likes_string)
    p2.wrapOn(p, 400, 1000)
    p2.drawOn(p, 110, 600)
    p.save()
    buffer.seek(0)

    new_dislike.pdf_dislike.save(f"post{interactions_total}.pdf", buffer)
    new_dislike.save()

    return redirect('/')


I know the code is quite messy.But where does the error come from?

Problem is you deleted in models.py, migration but forgot delete in database.

You can manually delete it in db, or do it with commands console

Assum you have 2 migrations in app test_app:

  1. 0001_test_1.py

  2. 0002_test_2.py #have column num_interactions_total

Now want delete num_interactions_total. You can revert migrations like:

python manage.py migrate test_app 0001_test_1

It will back your database to migration 0001_test_1, revert all changed in 0002_test_2 - delete column num_interactions_total

Back to Top