Python Django sees old migrations but everything was deleted
I cleared all my database, even created new with different name, deleted all migrations and migrations folder in every app of my django project, cleared all images, containers, volumes and builds in my Docker. But when I am running
python manage.py showmigrations
it shows me my old migrations, but I have no idea where does it finds them, because I deleted everything I could.
and when I am running
python manage.py makemigrations
it returns me message "No changes detected"
I even changed database name to new one.
I deleted everything and just wanted to start from zero, but somehow it finds old migrations that doesn't even exist. enter image description here
delete all migration files (except for init.py)
then clear cache (run this command on your terminal where your django project is)
find . -path "*/__pycache__/*" -delete
Drop and Recreate the Database if need be
Reset Docker Environment
then make migration
python manage.py makemigrations
python manage.py migrate
Django save a table on database with the migrations, you can delete this table, but study before to make sure are you doing.
Django uses a database table called django_migrations
to store information about the migrations. If python manage.py showmigrations
still shows the migrations after you droped the database then you could check the following:
- Maybe you droped another database
- Maybe the
django_migrations
table is in other database (duplicated databases or other weird stuff) - Some kind of caching problem, as others mentioned, delete the
*.pyc __pycache__
files
You could also try faking the migrations. If the db was not actually dropped, you are going to have errors. It will be a confirmation that the database was not correctly droped.
python manage.py migrate --fake <app_name> zero
Django stores migration history in table djnago_migrations in DB, that's why you're seeing this error
First of all delete all your migrations, you can use this following command
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
Then unistall django and the again install it. Once it's done open your python db shell and delete migrations table from your DB and then create new fresh migrations and then migrate
python manage.py dbshell
DROP TABLE django_migrations;
python3 manage.py makemigrations
python3 manage.py migrate
This is the most effective way I found out to get rid out of this error. Note Just be cautious while doing this take your DB backup first before doing this