如何修复Django中的迁移错误(不是无效约束失败)



您好,谢谢您的时间

情况:

我需要在我的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:python35libsite-packagesdjangodbbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:python35libsite-packagesdjangodbbackendssqlite3base.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)JetBrainsPyCharm 5.0.4helperspycharmdjango_manage.py", line 41, in <module>
    run_module(manage_file, None, '__main__', True)
  File "C:python35librunpy.py", line 182, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:python35librunpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:python35librunpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "E:/liemanmanage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:python35libsite-packagesdjangocoremanagement__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:python35libsite-packagesdjangocoremanagement__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:python35libsite-packagesdjangocoremanagementbase.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:python35libsite-packagesdjangocoremanagementbase.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:python35libsite-packagesdjangocoremanagementcommandsmigrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:python35libsite-packagesdjangodbmigrationsexecutor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:python35libsite-packagesdjangodbmigrationsexecutor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:python35libsite-packagesdjangodbmigrationsexecutor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:python35libsite-packagesdjangodbmigrationsmigration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:python35libsite-packagesdjangodbmigrationsoperationsfields.py", line 62, in database_forwards
    field,
  File "C:python35libsite-packagesdjangodbbackendssqlite3schema.py", line 221, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:python35libsite-packagesdjangodbbackendssqlite3schema.py", line 189, in _remake_table
    self.quote_name(model._meta.db_table),
  File "C:python35libsite-packagesdjangodbbackendsbaseschema.py", line 110, in execute
    cursor.execute(sql, params)
  File "C:python35libsite-packagesdjangodbbackendsutils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:python35libsite-packagesdjangodbbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:python35libsite-packagesdjangodbutils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:python35libsite-packagesdjangoutilssix.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:python35libsite-packagesdjangodbbackendsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:python35libsite-packagesdjangodbbackendssqlite3base.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

如何解决此问题?我知道,我可以删除所有迁移并进行。但是我做不到,因为我将删除有关此模型的所有信息。

您的迁移可能不会为在DB中创建的将具有此字段的对象设置值。您可能必须删除最后一个迁移文件,在模型字段中设置null = true并再次运行迁移。

最新更新