列名重复

时间:2016-03-15 16:08:26

标签: python django

我已经改变了我的模型然后我尝试迁移它们,但是出现了这个错误:

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'")

这是我的模特:

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')

怎么了?

迁移文件

# -*- 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'),
        ),
    ]

7 个答案:

答案 0 :(得分:9)

我有同样的问题。基本上,原因是迁移认为数据库具有那些列但数据库实际上没有,因此您需要一个过程来从迁移记录中删除那些不存在的列。

1.在您的代码中评论这些列。

2.重置迁移。

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

3.正常的初始迁移。

python manage.py makemigrations
python manage.py migrate

4.在您的代码中,取消注释这些重复的列。

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

5.现在您的迁移和代码在同一页面上。我使用假来让迁移相信DB有这些列,但实际上DB没有。

6.在您的代码中,再次注释这些重复列。

python manage.py makemigrations
python manage.py migrate

7.在这里,您成功从迁移中删除了这些列记录。在您的代码中,这些列也会被评论。它们在同一页上。

8.在代码中再次取消注释这些列并进行迁移。

python manage.py makemigrations
python manage.py migrate

9.然后应该工作。

这是对我的情况有用的唯一方法。我希望其他人可以提供更简单的方法。

答案 1 :(得分:3)

我将假设您之前已经添加了short_description_eng字段的迁移。您可以通过查看字符串'short_description_eng'的先前迁移文件来检查这一点。如果那是True,您只需从0002_auto_20160315_1544迁移文件中删除以下内容即可。

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

如果情况并非如此,那么您可能已经将数据库置于foobar'ed状态,并且可能值得删除它(假设这是一个开发数据库)并重新创建它。

答案 2 :(得分:2)

您试图运行的迁移给人的印象是,数据库中确实存在某个特定的列,但实际上该列已删除,因为您已将其删除了。因此,您需要通过伪造来回滚迁移,以“说服”数据库其中存在迁移,然后重新应用迁移。

假设您在应用myapp中进行了这些迁移:

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

您将需要将迁移回滚到该列仍然存在的点。请注意,如果您使用的是prod数据库,则应注意这样做可能造成的潜在数据丢失。

如果您删除了0003_removedcolumn.py中的列,则需要运行:python manage.py migrate --fake myapp 0002_something

然后,您可以重新运行顺序迁移:python manage.py migrate myapp。您的问题现在应该解决!

从这里开始,您可以运行python manage.py makemigrations,它应该可以平稳运行。

答案 3 :(得分:1)

刚刚找到解决方案!我使用0001_initial删除了所有迁移,然后运行makemigrationsmigrate并完成了所有更改!

答案 4 :(得分:1)

当我遇到此问题时,我不得不进行每次迁移,表示它有一个重复的列并将0002_*.py迁移文件与0001_initial.py迁移文件合并。

1。复制operations = []列表中的所有操作,并将其粘贴到operations = []文件中的0001_initial.py列表中。

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。如果除0001_initial之外还有其他依赖项,则可以从依赖项列表中复制dependencies = []中的所有行(除了显示('myapp', '0001_initial'),的行除外)并将其粘贴到{{1迁移文件。

重要提示:确保您也获取任何导入依赖项。按照这些简单的步骤后我没有遇到任何问题。

答案 5 :(得分:1)

我遇到了同样的问题。

我刚刚尝试了https://github.com/BirkbeckCTP/janeway/issues/451中的python3 manage.py migrate appname --fake,它对我有用。

答案 6 :(得分:0)

当我重新运行部分应用的迁移脚本时,我看到了这个问题。解决方案是临时删除添加了新模式已创建的列的行(有些hacky,但适用于开发环境数据库)