Duplicate column name

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.


The migration you are trying to run is under the impression that a certain column exists in the database that isn't actually there, since you removed it at some point. So you'll need to roll back the migrations through faking to "convince" the database that the migration is there, and then reapply the migrations.

Say you have these migrations in your app myapp:

  • 0001_initial.py
  • 0002_something.py
  • 0003_removedcolumn.py
  • 0004_anotherthing.py

You're going to need to roll back the migrations back to a point when the column still existed. Note that if you are in a prod database, you should be aware of potential data loss that could come from doing this.

If you removed the column in 0003_removedcolumn.py, you'll need to run: python manage.py migrate --fake myapp 0002_something.

Then, you can rerun the sequential migrations: python manage.py migrate myapp. Your problem should be solved now!

From here, you can run python manage.py makemigrations and it should run smoothly.

Tags:

Python

Django