Duplicate column name

后端 未结 7 1890
栀梦
栀梦 2021-02-05 15:22

I\'ve changed my models an then I tried to migrate them, but got this error:

python manage.py migrate
Operations to perform:
  Apply all migrations: admin, conte         


        
7条回答
  •  遇见更好的自我
    2021-02-05 16:01

    I had the same issue. Basically, the reason is because the migration thinks the database has those columns but the DB actually does not, so you need a procedure to delete those non-existent columns from migration records.

    1.Comment those columns in your code.

    2.Reset migrations.

    find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
    find . -path "*/migrations/*.pyc"  -delete
    

    3.Do normal initial migration.

    python manage.py makemigrations
    python manage.py migrate
    

    4.In your code, uncomment those duplicate columns.

    python manage.py makemigrations
    python manage.py migrate --fake
    

    5.Now your migrations and code are on same page. I use fake to let migration believe DB has those column, but indeed DB does not.

    6.In your code, comment those duplicate column again.

    python manage.py makemigrations
    python manage.py migrate
    

    7.Here, you successfully delete those column records from migrations. And also in your code, those column are commented. They are on the same page.

    8.Uncomment those columns again in your code and do migrations.

    python manage.py makemigrations
    python manage.py migrate
    

    9.It should then work.

    This is the only way that worked for my situation. I hope others can provide an easier approach.

提交回复
热议问题