'OperationalError at / no such table: blog_post_categories

I'm trying to add category section to my blog pet-project; I feel that I almost there, but on the last part of work it showed me an error: "OperationalError at / no such table: blog_post_categories"

My models.py

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    categories = models.ManyToManyField('Category', related_name='posts')


    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})


    class Category(models.Model):
        name = models.CharField(max_length=20)

My views.py

def home(request):
    content = {
        'posts': Post.objects.all()
    }

    return render(request, 'blog/home.html', content)

def blog_category(request, category):
    posts = Post.objects.filter(
        categories__name__contains=category
    ).order_by(
        '-created_on'
    )
    content = {
        'category': category,
        'posts': posts
    }
    return render(request, 'blog/blog_category.html')

I don't really understand why, but debugger shows that it is something wrong in base.html on line 0 "In template /media/john/DATA/DJANGO/WORKING/blog/templates/blog/base.html, error at line 0"

some code from the base.html template

1   {% load static %}
2   <!DOCTYPE html>
3   <html lang="en" dir="ltr">
4     <head>
5       <meta charset="utf-8">
6       <meta name="viewport" content="width=device-width, initial-scale=1.0">
7       <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
8       <link rel="stylesheet" href="{% static 'blog/main.css' %}">
...


Solution 1:[1]

So, after a day I get bored with bug hunting and solving nothing.

The solution for my bug was deleting the Database (rm db.sqlite3) and running python3 manage.py migrate

Hope it will help someone else with this bug.

P.S: I don't really know why this bug occurred, if someone have an answer, please let me know, I'm so curious about this small but annoying bug:)

Solution 2:[2]

For the "OperationalError at / no such table: blog_post_categories" - I think you might be missing migrations (manage.py makemigrations / manage.py migrate).

Solution 3:[3]

this error occures because of invalid schema or unsupported characters involved in input that you are passing. so for this you just have to update your table type by running following query

So by this query you are changing the character set of the column. Even though your database has a default character set of utf-8 I think it's possible for database columns to have a different character set in MySQL. Here's the SQL QUERY I used:

reference

ALTER TABLE database.table MODIFY COLUMN col VARCHAR(255)
    CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 C O D E
Solution 2 Alex
Solution 3 TarangP