Django Programming error column does not exist even after running migrations

前端 未结 9 1021
南旧
南旧 2020-12-08 20:54

I run python manage.py makemigrations and I get: No changes detected Then, python manage.py migrate and I get: No migrations to apply.

相关标签:
9条回答
  • 2020-12-08 21:23

    So, I always run into this sort of problem, so today I decided to try and work it out at the database level. Thing is, I changed a model field name which Django didn't bother reflecting in a migration file. I only found out later when I ran into problems. I later looked at the migration files and discovered there was no migration for that change. But I didn't notice because I made other changes as well, so once I saw a migration file I was happy. My advice. Create migration for each change one at a time. That way you get to see if it happened or not.

    So here's my working through it in MySQL.

    open mysql console.

    show databases; # see all my dbs. I deleted a few
    drop database <db-name>; # if needed
    use <db-name>; # the database name for your django project
    show tables; # see all tables in the database
    DESCRIBE <table-name>; # shows columns in the database
    SHOW COLUMNS FROM <db-name>; # same thing as above
    ALTER TABLE <table-name> CHANGE <old-column-name> <new-column-name> <col-type>; # now I manually updated my column name
    

    If you're using postgresql, just google the corresponding commands.

    0 讨论(0)
  • 2020-12-08 21:24

    When I get this error, my extreme way to solve it is to reset my database:

    1. Reset your database

    For Postgresql on Heroku:

    Heroku > your_app > Resources > database > add-ons > click on your database and open it

    For postgresql

    settings > Reset database

    1. Delete all files in your_app > migrations > __pycache__ except __init.py__
    2. Delete all files in your_app > migrations except __pycache__ folder and __init.py__
    3. Then run:

      python manage.py makemigrations

      python manage.py migrate

      python manage.py createsuperuser

    type in to create your superuser, then run:

    python manage.py makemigrations
    
    python manage.py migrate
    
    python manage.py 
    

    If you are able to inspect your models from your admin section, then it should be all okay now.

    0 讨论(0)
  • 2020-12-08 21:28

    Django migrations are recorded in your database under the 'django_migrations' table. This is how Django knows which migrations have been applied and which still need to be applied.

    Have a look at django_migrations table in your DB. It may be that something went wrong when your migration was applied. So, delete the row in the table which has the migration file name that is related to that column that 'does not exist'. Then, try to re-run a migration.

    0 讨论(0)
提交回复
热议问题