20

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, contenttypes, auth, sessions, myapp
Running migrations:
  Rendering model states... DONE
  Applying myapp.0002_auto_20160315_1544...Traceback (most recent call last):
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1060, "Duplicate column name 'short_description_eng'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/backends/mysql/schema.py", line 50, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 396, in add_field
    self.execute(sql, params)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/home/bootuz/final/myvenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1060, "Duplicate column name 'short_description_eng'")

This is my models:

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

Whats wrong?

Migration file

# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-03-15 15:44
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0001_initial'),
    ]

    operations = [
        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'),
        ),
    ]
Sayse
  • 42,633
  • 14
  • 77
  • 146
Bootuz
  • 530
  • 3
  • 6
  • 16

8 Answers8

20

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.

jiashenC
  • 1,812
  • 2
  • 16
  • 31
  • Why does the migration script "think" that the fields already exist? Maybe a bug and should be reported? – Joel G Mathew Sep 19 '18 at 02:27
  • @Droidzone I got the issue because I share the `migration` files with my coworkers. He successfully runs migrations on his local machine, but his migrations have different records as my localhost database schema. – jiashenC Sep 19 '18 at 19:13
  • @JoelGMathew, the migration script thinks the fields exist because you removed them at some point outside of a migration. It's not a bug. – CodeBiker Jun 25 '20 at 16:57
  • In my case I had some stale, duplicate `Email` column but executing steps 1-3 by themselves fixed it. – Zenul_Abidin Dec 17 '21 at 11:46
12

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.

CodeBiker
  • 2,985
  • 2
  • 33
  • 36
6

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.

Arter Tendean
  • 108
  • 2
  • 7
4

I'm going to assume that you have a previous migration in which you've already added the short_description_eng field. You could check this by looking through the previous migration files for the string 'short_description_eng'. If thats True, you can just delete the following from the 0002_auto_20160315_1544 migration file.

migrations.AddField(
    model_name='words',
    name='short_description_eng',
    field=models.CharField(blank=True, default='', max_length=100, verbose_name='Условное обозначение Eng'),
),

If that isn't the case then its possible you've got your database in a foobar'ed state and it may be worth deleting it (assuming this is a development database) and recreating it.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Can i backup my datas and then recreate the db and then restore the data? – Bootuz Mar 15 '16 at 16:30
  • 1
    @AstemirBoziev - Possibly, with [`dumpdata`](https://docs.djangoproject.com/en/1.9/ref/django-admin/#dumpdata) and [`loaddata`](https://docs.djangoproject.com/en/1.9/ref/django-admin/#loaddata), although its probably safer to first create a separate database and change your settings to use this new database to see if it rectifies the problem. – Sayse Mar 15 '16 at 16:32
1

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.

Blairg23
  • 11,334
  • 6
  • 72
  • 72
1
  • Remove the field of 'short_description_tur' from DB by using the bellow script:

    Alter table Words Drop short_description_tur;

  • And then migrate

0

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

Bootuz
  • 530
  • 3
  • 6
  • 16
  • 8
    Deleting all migrations is not a good solution especially if it is production database.You could have try faking your migrations – Ankush Nov 02 '16 at 09:15
0

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)

kip2
  • 6,473
  • 4
  • 55
  • 72