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

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

i got this error, webapp_cart is a model i made. But later i realise that i dont need that table so i deleted on my models.py and in my databse the table was there, so i deleted that table using DB browser(sqlite) but why i am getting this error. " no such table found"

Most likely, the problem is that you didn’t do a makemigrations / migrate when deleting the model.

If you look at your database, you’ll see that there’s a “django_contenttype” table which tracks models and tables.

Django still thinks that the table exists.

I don’t know of an easy way to fix this - all I can think of doing would be to restore your code with the model and add the table back into the database. Run your system to verify that everything works, then delete the model from the code and do the makemigrations / migrate.

how can i restore my code with the model.

Pull it from your git repo or whatever version control software you’re using, or failing that, recover it from a backup.

@Karki1234 As @KenWhitesell already mentioned, django_contenttype thinks the table already exists but you manually deleted that, so it broke your migration history and it would require some work to fix manually. If it is possible for you, you can change your database to a fresh new one.

In your settings.py file change the name of your database. For example, You have to change name mydatabase below.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
    }
}

After this you can run the migrations again with python manage.py migrate and your new database will be ready to work. With this, you should be able to run your app I believe.

1 Like

I found another solution:
Delete pycache and db.sqlite3
Is this correct? I havent tried because i am afraid doing this may cause other errors.

1 Like

yeah this worked but i think my admin password and username also changed or something happened i cannot go inside my admin pannel. i tried to reset password using:

so now what should i do.

If you deleted db.sqlite3, then you’re starting from scratch. You’ll need to rerun your migrations and recreate all your data, including your user accounts.

1 Like

Specify the appname when running migrations

  • python manage.py makemigrations appname

  • python manage.py migrate appname

2 Likes