简体   繁体   English

迁移时的python django问题

[英]python django issue with the migrations

Is there any way to deal with the migrations ? 有什么办法可以解决迁移问题?

I have been working with the version 1.8 of django, where, after doing any change in the models.py, we need to run the following commands - 我一直在使用django的1.8版,其中在models.py中进行了任何更改之后,我们需要运行以下命令-

python manage.py makemigrations
python manage.py migrate

Many a times, this gives an error. 很多时候,这会导致错误。 And it so happens that I have to rebuild the project as there is no way out. 碰巧的是,由于没有出路,我不得不重建项目。

I also tried the following way-outs, but none of them worked. 我也尝试了以下解决方法,但没有一个起作用。

  • deleted the migrations folder 删除了迁移文件夹
  • undo the changes to model.py 撤消对model.py的更改
  • deleted the files inside the migrations folder 删除了迁移文件夹中的文件
  • tried with flushing, squashing the migratios 尝试冲洗,压扁the

it every times shows the following error with a very long error log of some unknown files. 它每次都会显示以下错误,并带有一些未知文件的非常长的错误日志。

Post Edit : Here's the whole log 发表编辑:这是整个日志

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 constraint failed: zapp_post__new.specs_order_post_id django.db.utils.IntegrityError:NOT NULL约束失败:zapp_post__new.specs_order_post_id

Here is the link to my project, which currently is showing the error. 这是我的项目的链接 ,当前正在显示错误。 You can try running the application 您可以尝试运行该应用程序

What should be done in this case ? 在这种情况下应该怎么做?

Downloaded your code, deleted db.sqlite3, ran syncdb, everything worked fine. 下载代码,删除db.sqlite3,运行syncdb,一切正常。 Since you didnt have any sensitive data in your db i think that works for you. 由于您的数据库中没有任何敏感数据,因此我认为这很适合您。

Here's a little extra info for future: 这是将来的一些额外信息:

When modifying migrations/DB manually or when you run into a problem with migrations you should consider these things: 手动修改迁移/数据库或遇到迁移问题时,应考虑以下事项:

  • You should NOT delete migrations folder 您不应该删除迁移文件夹
  • Migrations folder should always contain __init__.py file 迁移文件夹应始终包含__init__.py文件
  • All applied migrations are stored in django_migrations table, so if you delete all migration files and remake migrations (ie creating a new 0001_initial.py), running migrate wont do anything, because django thinks it's already applied 所有已应用的迁移都存储在django_migrations表中,因此,如果删除所有迁移文件并重新进行迁移(即创建新的0001_initial.py),则运行迁移将无济于事,因为django认为已应用该迁移。
  • Sometimes deleting specific rows in django_migrations table and also modifying your tables structure (according to deleted rows) solves the problem, but you should know what you're doing. 有时删除django_migrations表中的特定行并修改表结构(根据已删除的行)可以解决问题,但是您应该知道自己在做什么。

So, the easiest solution when you run into a problem with migrations is deleteing all files in migrations folder (except __init__.py ), deleting all rows in django_migrations table where app=your_app_name, droping all tables of your app, then remaking migrations and applying them. 因此,遇到迁移问题时,最简单的解决方案是删除迁移文件夹中的所有文件( __init__.py除外),删除django_migrations表中app = your_app_name的所有行,删除应用程序的所有表,然后重新进行迁移并应用他们。

But if you have sensitive data and you can't delete db, it gets more complicated 但是,如果您有敏感数据并且无法删除数据库,它将变得更加复杂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM