如何在django中修复迁移错误(NOT NULL约束失败)?

时间:2016-12-17 08:42:56

标签: django django-models

您好,感谢您的时间

情况:

我需要在我的django模型中添加一个新字段

字段

status_of_lieman = models.BooleanField(default=False, verbose_name="Course status")

Makemigratios很好,但是当我尝试迁移时,我得到错误:

Running migrations:
  Applying faceset.0077_course_status_of_lieman...Traceback (most recent call last):
  File "C:\python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\python35\lib\site-packages\django\db\backends\sqlite3\base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: faceset_course.status_of_lieman

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pycharm\django_manage.py", line 41, in <module>
    run_module(manage_file, None, '__main__', True)
  File "C:\python35\lib\runpy.py", line 182, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:\python35\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\python35\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "E:/lieman\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\python35\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\python35\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\python35\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\python35\lib\site-packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:\python35\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:\python35\lib\site-packages\django\db\migrations\executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\python35\lib\site-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 "C:\python35\lib\site-packages\django\db\migrations\executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\python35\lib\site-packages\django\db\migrations\migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\python35\lib\site-packages\django\db\migrations\operations\fields.py", line 62, in database_forwards
    field,
  File "C:\python35\lib\site-packages\django\db\backends\sqlite3\schema.py", line 221, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:\python35\lib\site-packages\django\db\backends\sqlite3\schema.py", line 189, in _remake_table
    self.quote_name(model._meta.db_table),
  File "C:\python35\lib\site-packages\django\db\backends\base\schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "C:\python35\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\python35\lib\site-packages\django\db\utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\python35\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\python35\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\python35\lib\site-packages\django\db\backends\sqlite3\base.py", line 323, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: faceset_course.status_of_lieman

我该如何解决这个问题?我知道,我可以删除所有迁移并执行此操作。但我无法做到,因为我将删除此模型的所有信息。

1 个答案:

答案 0 :(得分:0)

您的迁移可能没有为数据库中已创建的具有此字段的对象设置值。您可能必须删除上一个迁移文件,在模型字段中设置null = True并再次运行迁移。