Django 从失败的迁移中恢复
跑步时Django 从失败的迁移中恢复,django,django-migrations,Django,Django Migrations,跑步时 python3 manage.py migrate 有人问我“id”的默认值应该是多少,然后输入1。我读过 但这对我来说有点太复杂了,所以我尝试了1 现在当我跑的时候 python3 manage.py migrate 我得到以下错误: vagrant@vagrant-ubuntu-trusty-64:/vagrant/grader$ python3 manage.py migrate Operations to perform: Apply all migrations: a
python3 manage.py migrate
有人问我“id”的默认值应该是多少,然后输入1
。我读过
但这对我来说有点太复杂了,所以我尝试了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"
它返回了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)
我经常遇到类似的问题,迁移由于任何原因而失败,并且只有部分迁移应用于数据库 理论上,您应该能够进入数据库并运行查询以完成迁移,但根据我的经验,很难知道要运行哪些确切的查询以及迁移的哪些部分尚未运行 我发现最可靠的修复方法是伪造迁移,然后备份模型,在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在迁移失败之前完成了多远。我经常遇到类似的问题,迁移由于任何原因而失败,并且只有一部分应用于数据库 理论上,您应该能够进入数据库并运行查询以完成迁移,但根据我的经验,很难知道要运行哪些确切的查询以及迁移的哪些部分尚未运行 我发现最可靠的修复方法是伪造迁移,然后备份模型,在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在迁移失败之前经过了多远。请在
核心应用程序中向我们展示学生
模型。您在学生模型中提供了您自己的id字段吗?id是由迁移自动创建的。我已将学生模型粘贴到问题底部的编辑中。请在core
app中显示Student
模型。您是否在学生模型中提供了自己的id字段?id是通过迁移自动创建的。我已将学生模型粘贴到问题底部的编辑中。