2016-01-03 82 views
0

恢復當運行從失敗的遷移

python3 manage.py migrate 

有人問我「身份證」的默認值應該是什麼,進入1。我讀過

https://docs.djangoproject.com/en/1.9/howto/writing-migrations/#migrations-that-add-unique-fields

但有點過於複雜,對我來說嘗試1

現在,當我運行

python3 manage.py migrate 

我得到以下錯誤:

[email protected]:/vagrant/grader$ python3 manage.py migrate 
Operations to perform: 
    Apply all migrations: admin, core, contenttypes, auth, sessions 
Running migrations: 
    Rendering model states... DONE 
    Applying core.0002_auto_20160103_1302...Traceback (most recent call last): 
    File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
psycopg2.ProgrammingError: multiple default values specified for column "id" of table "core_student" 


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 "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line 
    utility.execute() 
    File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 342, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 348, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 399, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 200, in handle 
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) 
    File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 92, in migrate 
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial) 
    File "/usr/local/lib/python3.4/dist-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 "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/migration.py", line 123, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards 
    field, 
    File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 396, in add_field 
    self.execute(sql, params) 
    File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 110, in execute 
    cursor.execute(sql, params) 
    File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 95, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "core_student" 

如何從這種狀態中恢復失敗的遷移?我走進PSQL命令PROMT和類型

SELECT * FROM core_students 

它返回0行,所以我don'w知道爲什麼我有一個問題。

不應該的Django自動進行「ID」字段是唯一的號碼?

編輯:

的ID已經由Django的遷移自動生成。

學生模型:

class Student(models.Model): 
    created = models.DateTimeField(auto_now_add=True) 
    updated = models.DateTimeField(auto_now=True) 

    owner = models.ForeignKey('auth.User', related_name='students') 

    identity_number = models.CharField(max_length=50) 
    first_name = models.CharField(max_length=50) 
    last_name = models.CharField(max_length=50) 
+1

請告訴我們在'core'應用 – masnun

+0

你在學生模型提供自己的ID字段'Student'模式? – doniyor

+0

該ID是由遷移自動創建的。我在問題的底部粘貼了學生模型進行編輯。 – user1283776

回答

0

我經常碰到類似的問題,即不管是什麼原因遷移失敗,而只是其中的一部分被應用到數據庫中。

從理論上講,你應該能夠進入數據庫,並運行查詢來完成遷移,但我們知道什麼確切的查詢執行時間及其所遷移的部分尚未運行,難以在我的經驗。

我發現修復它的最可靠的方法是僞造遷移,然後備份模型,在models.py中將其註釋掉,然後進行遷移以刪除它,然後假冒它。然後,我可以進入數據庫,刪除表格,然後進行新遷移,以現在我想要的方式重新創建它。 這裏是我的命令運行:

python manage.py migrate --fake [appname] #fake the failed migration 
#comment out the model in models.py and back up the data 
python manage.py makemigrations [appname] 
python manage.py migrate --fake [appname] #fake the migration to delete 
the table 
python manage.py dbshell 
#drop the table. Mysql would be: DROP TABLE [appname]_[modelname]; 
#exit dbshell 
#Uncomment the model in models.py adding in whatever changes were originally wanted 
python manage.py makemigrations [appname] 
python manage.py migrate #assuming your model change is valid, this should work this time. 
#reload your data into the table 

不,這不是優雅,但它得到的Django遷移,而不必去猜測遠的Django通過遷移是怎麼出現故障之前再次合作。