Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

Django Programming error column does not exist even after running migrations

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

Then, I try to push the changes to production: git push heroku master Everything up-to-date

Then, in production, I repeat the command: heroku run python manage.py migrate No migrations to apply.

Just in case, I run makemigrations in production:

heroku run python manage.py makemigrations
No changes detected

WHY then I get a

ProgrammingError at ....

column .... does not exist

"No changes detected" means the database is coherent with the code. How can I debug this???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got the same problem (column not exist) but when I try to run migrate not with makemigrations (it is the same issue I believe)

  • Cause: I removed the migration files and replaced them with single pretending intial migration file 0001 before running the migration for the last change

  • Solution:

    1. Drop tables involved in that migration of that app (consider a backup workaround if any)
    2. Delete the rows responsible of the migration of that app from the table django_migrations in which migrations are recorded, This is how Django knows which migrations have been applied and which still need to be applied.

And here is how solve this problem:

  • log in as postgres user (my user is called posgres):

    sudo -i -u postgres

  • Open an sql terminal and connect to your database:

    psql -d database_name

  • List your table and spot the tables related to that app:

    dt

  • Drop them (consider drop order with relations):

    DROP TABLE tablename ;

  • List migration record, you will see migrations applied classified like so:

id | app | name | applied
--+------+--------+---------+

SELECT * FROM django_migrations;
  • Delete rows of migrations of that app (you can delete by id or by app, with app don't forget 'quotes'):

    DELETE FROM django_migrations WHERE app='yourapp';

  • log out and run your migrations merely (maybe run makemigrations in your case):

    python manage.py migrate --settings=your.settings.module_if_any

Note: it is possible that in your case will not have to drop all the tables of that app and not all the migrations, just the ones of the models causing the problem.

I wish this can help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...