0

I've created a new attribute level in my model. I've forgot to put there null=True before migrations. When the cmd asks whether I want to put a default value, I've typed None. Now, I can't do migrate. So I tried to remove level column and do new makemigrations but still the same problem happens when I try to call migrate.

django.db.utils.IntegrityError: MyApp__job__new.level_id may not be NULL

Is there a way to get back to the state when there was no level attribute?

makemigrations:

Migrations for 'SolutionsForLanguagesApp':
  0019_remove_job_level.py:
    - Remove field level from job

migrate:

(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

SHOWMIGRATIONS:

(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

MIGRATION 0013:

# -*- 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),
        ),
    ]
Milano
  • 18,048
  • 37
  • 153
  • 353

1 Answers1

1

Since you haven't applied migrations 0013 to 0019, the easiest thing to do might be to delete these files, then rerun makemigrations (check in a copy of the files if you're using version control or take a backup first).

If you need more fine grained control, you could edit the 0013 migration which is failing, and set null=True to prevent the IntegrityError.

    migrations.AddField(
        model_name='job',
        name='level',
        field=models.ForeignKey(default=None, blank=True, null=True, to='SolutionsForLanguagesApp.Level'),
        preserve_default=False,
Alasdair
  • 298,606
  • 55
  • 578
  • 516