'OperationalError in Django app: models.py
There's an OperationalError in my models.py code. Can anyone pinpoint the problem? I have tried deleting the cache (along with previous migrations) and doing them over - doesn't help the issue.
from django.db import models
# Create your models here.
class Topic(models.Model):
"""A post within a blog."""
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of a model."""
return self.text
class Post(models.Model):
"""Class for Blog Post entries."""
topic = models.ForeignKey(Topic)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'posts'
def __str__(self):
"""Return a string representation of the model."""
return self.text[:50] + "..."
It gives me the following errors, if this helps:
OperationalError at /admin/blogs/post/
no such table: blogs_post
admin.py:
from django.contrib import admin
# Register your models here.
from blogs.models import Topic, Post
admin.site.register(Topic)
admin.site.register(Post)
Solution 1:[1]
If you changed a class name, then you probably have objects with the old class name that the migration can't find the table for.
Delete the db.sqlite3
file at project folder. Delete all previous migrations. Then makemigrations
then migrate
. This basically resets everything and deletes all the objects. You have to recreate your Superuser afterwards also.
Hope this helps,.
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 | almost a beginner |