Poornaka
Poornaka

Reputation: 180

An operational error when making django AJAX likes

This is the error that I got.

django.db.utils.OperationalError: no such table: photos_post_likes

I was following this video to create a like button. https://youtu.be/pkPRtQf6oQ8

By then I had already created the model (Post). Just when I added a new line (likes) in models.py and ran makemigrations, it migrated without any problem. But, when I ran migrate this error appeared.

Here is the models.py file

from django.db import models
from django.utils import timezone
from django.urls import reverse
from django.contrib.auth.models import User
from PIL import Image

class Post(models.Model):
    date_shared = models.DateTimeField(default=timezone.now)
    caption = models.TextField(max_length=50)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    likes = models.ManyToManyField(User, blank=True, related_name='post_likes') # this line made the error occur
    image = models.ImageField(upload_to='post_images')

    def __str__(self):
        return self.caption

    def save(self):
        super().save()

        img = Image.open(self.image.path)

        if img.height > 800 or img.width > 800:
            output_size = (800, 800)
            img.thumbnail(output_size)
            img.save(self.image.path)

    def get_absolute_url(self):
        return reverse('home')

Upvotes: 0

Views: 42

Answers (1)

Bialomazur
Bialomazur

Reputation: 1151

try running the following command:

python manage.py migrate --fake

I won't waste time going into deep what the command does exactly, as you can find a great post about the topic right here: Django migrate --fake and --fake-initial explained

if the error still should occur, you should delete all files located inside your migrations folder (except for init.py), and after that delete all tables inside your Database.

Afterwards simply run:

python manage.py makemigrations
python manage.py migrate

again and everything should work. But bear in mind that by deleting your tables you will of course also lose all data/queries saved in those.

Let me know if that worked for you, because it did many times before for me.

Upvotes: 1

Related Questions