Duplicate column name

后端 未结 7 1802
栀梦
栀梦 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.

    0 讨论(0)
  • 2021-02-05 16:03

    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.

    0 讨论(0)
  • 2021-02-05 16:09

    I got the same problem.

    I just tried python3 manage.py migrate appname --fake from https://github.com/BirkbeckCTP/janeway/issues/451 and it's work for me.

    0 讨论(0)
  • 2021-02-05 16:11

    Just found the solution! I deleted all my migrations with 0001_initial, and then run makemigrations and migrate and all the changes apllied!

    0 讨论(0)
  • 2021-02-05 16:11

    I saw this problem when I re-ran a partially applied migrations scripts. The solution was to temporarily delete the lines that added the columns that had already been created by the new schema (somewhat hacky but works for development environment databases)

    0 讨论(0)
  • 2021-02-05 16:17

    When I had this issue, I had to go to each migration that said it had a duplicate column and merge the 0002_*.py migration file with the 0001_initial.py migration file.

    1. Copy all the operations in the operations = [] list and paste them into the operations = [] list in the 0001_initial.py file.

    migrations.AddField(
        model_name='words',
        name='short_description_eng',
        field=models.CharField(blank=True, default='', max_length=100, verbose_name='Условное обозначение Eng'),
    ),
    migrations.AddField(
        model_name='words',
        name='short_description_rus',
        field=models.CharField(blank=True, default='', max_length=100, verbose_name='Условное обозначение Рус'),
    ),
    migrations.AddField(
        model_name='words',
        name='short_description_tur',
        field=models.CharField(blank=True, default='', max_length=100, verbose_name='Условное обозначение Tür'),
    ),
    migrations.AlterField(
        model_name='words',
        name='audio',
        field=models.FileField(blank=True, upload_to='audio', verbose_name='Озвучка'),
    ),
    migrations.AlterField(
        model_name='words',
        name='english',
        field=models.TextField(blank=True, default='', verbose_name='English'),
    ),
    migrations.AlterField(
        model_name='words',
        name='russian',
        field=models.TextField(blank=True, default='', verbose_name='Русский'),
    ),
    migrations.AlterField(
        model_name='words',
        name='title',
        field=models.CharField(max_length=100, unique=True, verbose_name='Слово'),
    ),
    migrations.AlterField(
        model_name='words',
        name='turkish',
        field=models.TextField(blank=True, default='', verbose_name='Türkçe'),
    ),
    

    2. If you had other dependencies besides 0001_initial, you would copy all the lines in dependencies = [] (except the line that says('myapp', '0001_initial'),) migrations from the list of dependencies and paste them into the 0001_initial.py migration file.

    Important Note: Make sure you grab any import dependencies too. I had no issues after following those simple steps.

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