创建外键后出错:django.db.utils.IntegrityError:MyApp_job__new.level_id可能不为NULL

时间:2016-04-13 14:25:16

标签: django django-models django-migrations

我在模型中创建了一个新属性null=True。在迁移之前我忘记了None。当cmd询问我是否要设置默认值时,我输入了migrate。现在,我做不到level。因此,我尝试删除makemigrations列并执行新的migrate,但在尝试调用django.db.utils.IntegrityError: MyApp__job__new.level_id may not be NULL 时仍然会出现同样的问题。

level

当没有makemigrations: Migrations for 'SolutionsForLanguagesApp': 0019_remove_job_level.py: - Remove field level from job 属性时,有没有办法回到状态?

(venv) C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2>pyth
on manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: essay, true_false, super_inlines, staticfiles, mu
ltichoice, messages, quiz, django_tables2, crispy_forms
  Apply all migrations: admin, contenttypes, SolutionsForLanguagesApp, auth, ses
sions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying SolutionsForLanguagesApp.0013_auto_20160413_1615...Traceback (most re
cent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\core\management\__init__.py", line 354, in execute_fr
om_command_line
    utility.execute()
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\core\management\__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\core\management\base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\core\management\base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\core\management\commands\migrate.py", line 222, in ha
ndle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\migrations\executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=f
ake_initial)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\migrations\executor.py", line 148, in apply_migrat
ion
    state = migration.apply(state, schema_editor)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\migrations\migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\migrations\operations\fields.py", line 62, in data
base_forwards
    field,
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\backends\sqlite3\schema.py", line 179, in add_fiel
d
    self._remake_table(model, create_fields=[field])
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\backends\sqlite3\schema.py", line 147, in _remake_
table
    self.quote_name(model._meta.db_table),
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\backends\base\schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\ven
v\lib\site-packages\django\db\backends\sqlite3\base.py", line 318, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: SolutionsForLanguagesApp_job__new.level_id may n
ot be NULL

迁移:

(venv) C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2>pyth
on manage.py showmigrations SolutionsForLanguagesApp
SolutionsForLanguagesApp
 [X] 0001_initial
 [X] 0002_auto_20160401_1234
 [X] 0003_auto_20160402_0105
 [X] 0004_auto_20160402_0107
 [X] 0005_job_is_done
 [X] 0006_auto_20160408_1506
 [X] 0007_auto_20160411_1834
 [X] 0008_auto_20160413_1413
 [X] 0009_auto_20160413_1446
 [X] 0010_auto_20160413_1543
 [X] 0011_auto_20160413_1559
 [X] 0012_auto_20160413_1604
 [ ] 0013_auto_20160413_1615
 [ ] 0014_auto_20160413_1616
 [ ] 0015_auto_20160413_1616
 [ ] 0016_remove_job_level
 [ ] 0017_job_level
 [ ] 0018_auto_20160413_1627
 [ ] 0019_remove_job_level

SHOWMIGRATIONS:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        ('SolutionsForLanguagesApp', '0012_auto_20160413_1604'),
    ]

    operations = [
        migrations.AddField(
            model_name='job',
            name='level',
            field=models.ForeignKey(default=None, blank=True, to='SolutionsForLanguagesApp.Level'),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='job',
            name='customer',
            field=models.ForeignKey(related_name='orders', to=settings.AUTH_USER_MODEL, help_text='Z\xe1kazn\xedk'),
        ),
        migrations.AlterField(
            model_name='job',
            name='file',
            field=models.FileField(help_text='Upload file', null=True, upload_to=b'', blank=True),
        ),
        migrations.AlterField(
            model_name='job',
            name='price',
            field=models.FloatField(help_text='Cena', null=True, blank=True),
        ),
        migrations.AlterField(
            model_name='job',
            name='translator',
            field=models.ForeignKey(related_name='jobs', blank=True, to=settings.AUTH_USER_MODEL, help_text='Prekladate\u013e', null=True),
        ),
    ]

移民0013:

flatDir

1 个答案:

答案 0 :(得分:1)

由于您尚未应用迁移0013到0019,因此最简单的方法可能是删除这些文件,然后重新运行makemigrations(如果您正在使用版本控制,请检查文件的副本或采取先备份)。

如果您需要更精细的控制,可以编辑失败的0013迁移,并设置null=True以阻止IntegrityError

    migrations.AddField(
        model_name='job',
        name='level',
        field=models.ForeignKey(default=None, blank=True, null=True, to='SolutionsForLanguagesApp.Level'),
        preserve_default=False,