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.
3 Likes
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.
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
Running this commands in your terminal should be the key to the solution.
$ python manage.py makemigrations
$ python manage.py migrate --run-syncdb
$ python manage.py migrate
then, you can go to python manage.py shell and import your table, from app_name.models import class_name
a you could save it or see it when adding a new row.
1 Like
If you have made things wrong because of another db.sqlite3 file or deleting migrations folder files, than things that worked for me:
- make models like sqlite, like no extra modals in there, copy you current models and admin somewhere
- now run
python manage.py migrate
- now run
python manage.py makemigrations
, this might give errors like no table not field something
- then run And while migrating fake that migrations as your error might say table not found to delete using
python manage.py migrate --fake
, also try python manage.py migrate --fake-initial
- if old database has been fixed, then you can now copy new, modals and run
python manage.py makemigrations
, this will delete, create, add accd. to you new modal, finally run python manage.py migrate
migrations errors has been worst errors, if you dont have some old code, you might mess things up.
I am getting the impression that this error indicates or refers to a security vulnerability. However, I don’t know if it is server-side or client-side–most likely somewhere in between.
Boring Company?