0

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')
Poornaka
  • 180
  • 1
  • 13

1 Answers1

1

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.

Bialomazur
  • 1,122
  • 2
  • 7
  • 18
  • It didn't delete anything. Should I delete it manually now? – Poornaka Jan 18 '21 at 02:55
  • @Poornaka What exactly do you mean with 'It'? – Bialomazur Jan 18 '21 at 02:57
  • @Poornaka well, if the command 'python manage.py migrate --fake' did not solve the issue for you, then you should connect to your database and delete the tables manully via SQL. If you tell what kind of SQL-DB you're using I could even tell you how to do so. – Bialomazur Jan 18 '21 at 02:59
  • Wait...did you mean it will delete the migrations folder in the project or the app? (the command) – Poornaka Jan 18 '21 at 03:04
  • @Poornaka No, read carefully. Step 1: Delete all files except '__init__.py' inside the 'migrations' folder located in your app. Step 2: Manually delete all tables in your database. Step 3: Rerun the commands 'python manage.py makemigrations' and 'python manage.py migrate'. – Bialomazur Jan 18 '21 at 03:15
  • By did not work I mean : `python manage.py migrate --fake` works. But makemigrations command says that there are no changes detected. The same when running migrations.When I go to the admin page and try to edit a post. The error occurs. – Poornaka Jan 18 '21 at 07:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227473/discussion-between-poornaka-and-bialomazur). – Poornaka Jan 18 '21 at 08:05