【问题标题】:Recover from failed migration从失败的迁移中恢复
【发布时间】:2016-01-03 13:17:03
【问题描述】:

运行时

python3 manage.py migrate

有人问我“id”的默认值应该是什么,然后输入1。我读过

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

但这对我来说有点太复杂了,所以尝试了1

现在当我跑步时

python3 manage.py migrate

我收到以下错误:

vagrant@vagrant-ubuntu-trusty-64:/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 命令提示符并输入

SELECT * FROM core_students

它返回了 0 行,所以我不知道为什么会出现问题。

难道 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)

【问题讨论】:

  • 请在core 应用程序中向我们展示Student 模型
  • 您是否在 Student 模型中提供了自己的 id 字段?
  • ID 是由迁移自动创建的。我已将 Student 模型粘贴到问题底部的编辑中。

标签: django django-migrations


【解决方案1】:

我经常遇到类似的问题,无论出于何种原因,迁移都会失败,并且只有部分迁移到了数据库中。

理论上,您应该能够进入数据库并运行查询以完成迁移,但根据我的经验,很难知道要运行哪些确切的查询以及迁移的哪些部分尚未运行。

我发现修复它的最可靠方法是伪造迁移,然后备份模型,在 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 在迁移失败之前完成了多远。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2017-03-10
    • 2016-02-16
    相关资源
    最近更新 更多