python django 迁移问题
python django issue with the migrations
有什么办法可以处理迁移吗?
我一直在使用 django 1.8 版,在 models.py 中进行任何更改后,我们需要 运行 以下命令 -
python manage.py makemigrations
python manage.py migrate
很多时候,这会出错。碰巧没办法只好重新建工程
我也尝试了以下出路,但 none 成功了。
- 删除了迁移文件夹
- 撤消对 model.py 的更改
- 删除了迁移文件夹中的文件
- 尝试过冲洗,压缩迁移
它每次都显示以下错误以及一些未知文件的很长的错误日志。
Post 编辑:这是整个日志
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/schema.py", line 147, in _remake_table
self.quote_name(model._meta.db_table),
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/base/schema.py", line 111, in execute
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError:NOT NULL 约束失败:zapp_post__new.specs_order_post_id
这是我项目的 link,目前显示错误。您可以尝试 运行 应用程序
遇到这种情况应该怎么办?
下载了你的代码,删除了 db.sqlite3,运行 syncdb,一切正常。
由于您的数据库中没有任何敏感数据,我认为这对您有用。
这里有一些关于未来的额外信息:
手动修改 migrations/DB 或当您 运行 遇到迁移问题时,您应该考虑以下事项:
- 您不应删除迁移文件夹
- Migrations 文件夹应始终包含
__init__.py
文件
- 所有应用的迁移都存储在 django_migrations table 中,因此如果您删除所有迁移文件并重新制作迁移(即创建新的 0001_initial.py),运行ning migrate 不会做任何事情,因为 django 认为它已经被应用
- 有时删除 django_migrations table 中的特定行并修改 table 的结构(根据删除的行)可以解决问题,但您应该知道自己在做什么.
因此,当您 运行 遇到迁移问题时,最简单的解决方案是删除迁移文件夹中的所有文件(__init__.py
除外),删除 django_migrations [=38] 中的所有行=] 其中 app=your_app_name,删除您应用的所有 table,然后重新进行迁移并应用它们。
但是如果你有敏感数据又不能删除数据库,那就更复杂了
有什么办法可以处理迁移吗?
我一直在使用 django 1.8 版,在 models.py 中进行任何更改后,我们需要 运行 以下命令 -
python manage.py makemigrations
python manage.py migrate
很多时候,这会出错。碰巧没办法只好重新建工程
我也尝试了以下出路,但 none 成功了。
- 删除了迁移文件夹
- 撤消对 model.py 的更改
- 删除了迁移文件夹中的文件
- 尝试过冲洗,压缩迁移
它每次都显示以下错误以及一些未知文件的很长的错误日志。
Post 编辑:这是整个日志
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/schema.py", line 147, in _remake_table
self.quote_name(model._meta.db_table),
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/base/schema.py", line 111, in execute
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/Django-1.8.2-py2.7.egg/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError:NOT NULL 约束失败:zapp_post__new.specs_order_post_id
这是我项目的 link,目前显示错误。您可以尝试 运行 应用程序
遇到这种情况应该怎么办?
下载了你的代码,删除了 db.sqlite3,运行 syncdb,一切正常。 由于您的数据库中没有任何敏感数据,我认为这对您有用。
这里有一些关于未来的额外信息:
手动修改 migrations/DB 或当您 运行 遇到迁移问题时,您应该考虑以下事项:
- 您不应删除迁移文件夹
- Migrations 文件夹应始终包含
__init__.py
文件 - 所有应用的迁移都存储在 django_migrations table 中,因此如果您删除所有迁移文件并重新制作迁移(即创建新的 0001_initial.py),运行ning migrate 不会做任何事情,因为 django 认为它已经被应用
- 有时删除 django_migrations table 中的特定行并修改 table 的结构(根据删除的行)可以解决问题,但您应该知道自己在做什么.
因此,当您 运行 遇到迁移问题时,最简单的解决方案是删除迁移文件夹中的所有文件(__init__.py
除外),删除 django_migrations [=38] 中的所有行=] 其中 app=your_app_name,删除您应用的所有 table,然后重新进行迁移并应用它们。
但是如果你有敏感数据又不能删除数据库,那就更复杂了