2016-12-17 4 views
0

Hallo und Vielen Dank für Ihre ZeitWie zu beheben Migrationsfehler (NOT NULL Constraint fehlgeschlagen) in Django?

Situation:

ich ein neues Feld in meinem django Modell

das Feld

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

Makemigratios fein geht hinzufügen müssen, aber wenn Ich versuche zu migrieren, bekomme ich Fehler:

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 

Wie kann ich dieses Problem beheben? Ich weiß, dass ich alle Migrationen löschen kann und mache es. Aber ich kann es nicht tun, da ich alle Informationen über dieses Modell löschen werde.

+1

Können Sie die Migration anzeigen, die das Feld hinzufügt? – knbk

Antwort

0

Ihre Migration legt wahrscheinlich keinen Wert für die Objekte fest, die bereits in Ihrer Datenbank erstellt wurden und dieses Feld enthalten. Sie müssen wahrscheinlich die letzte Migrationsdatei löschen, in Ihrem Modellfeld null = True setzen und die Migrationen erneut ausführen.

Verwandte Themen