2016-03-15 83 views
5

我已經改變了我的模型的話,我試圖遷移他們,但得到這個錯誤:重複的列名

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'), 
     ), 
    ] 
+2

你可以顯示'0002_auto_20160315_1544'遷移文件嗎? – Sayse

+0

你能否顯示你的第一個('0001_initial')遷移文件? – GwynBleidD

+0

http://pastebin.com/wzyDZFSv – Bootuz

回答

0

剛剛找到解決方案!我刪除了所有我的遷移與0001_initial,然後運行makemigrationsmigrate和所有更改apllied!

+3

刪除所有遷移不是一個好的解決方案,特別是如果它是生產數據庫。您可以嘗試僞造您的遷移 – Ankush

3

我會假設你在你已經添加了short_description_eng場之前的遷移。您可以通過查看以前的遷移文件來檢查字符串'short_description_eng'。如果那是真的,您可以從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狀態,它可能是值得將其刪除(假設這是一個開發數據庫),並重新創建它。

+0

我可以備份我的數據,然後重新創建數據庫,然後恢復數據? – Bootuz

+1

@AstemirBoziev - 可能使用['dumpdata'](https://docs.djangoproject.com/en/1.9/ref/django-admin/#dumpdata)和['loaddata'](https://docs.djangoproject .com/en/1.9/ref/django-admin /#loaddata),儘管它可能比較安全,首先創建一個單獨的數據庫並更改設置以使用這個新數據庫來查看是否糾正了問題。 – Sayse

4

我有同樣的問題。基本上,原因是遷移認爲數據庫具有這些列,但不是,所以您需要一個過程從遷移記錄中刪除那些不存在的列。

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.它應該工作。

這是唯一適用於我的情況的方法。我希望別人能提供一個更簡單的方法。