Opened 4 years ago

Closed 4 years ago

#30297 closed Bug (duplicate)

When delete fields and unique together from model django fails on migrations

Reported by: ApaDoctor Owned by: nobody
Component: Migrations Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

You have some model:

class RandomModel(models.Model):
    class Meta:
        unique_together = ['field_1', 'field_2']

    field_1 = models.DateField()
    field_2 = models.CharField(max_length=10)
    field_3 = models.CharField(max_length=10)
    field_4 = models.CharField(max_length=10)

You create and run migrations for it(with makemigrations and migrate)

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='RandomModel',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('field_1', models.DateField()),
                ('field_2', models.CharField(max_length=10)),
                ('field_3', models.CharField(max_length=10)),
                ('field_4', models.CharField(max_length=10)),
            ],
        ),
        migrations.AlterUniqueTogether(
            name='randommodel',
            unique_together={('field_1', 'field_2')},
        ),
    ]

Then you want to change model a bit:

class RandomModel(models.Model):
    class Meta:
        unique_together = ['field_1', 'field_4'] # remove from here field_2 and add field_4

    field_1 = models.DateField()
    # Remove field_2
    # Remove field_3
    field_4 = models.CharField(max_length=10)

    

Then you run makemigrations. And it generate this migration:

class Migration(migrations.Migration):

    dependencies = [
        ('models', '0001_initial'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='randommodel',
            name='field_2',
        ),
        migrations.RemoveField(
            model_name='randommodel',
            name='field_3',
        ),
        migrations.AlterUniqueTogether(
            name='randommodel',
            unique_together={('field_1', 'field_4')},
        ),
    ]

Here you can see, that AlterUniqueTogether is put after RemoveField. So when you will run makemigrations - you will get:

Running migrations:
  Applying models.0002_auto_20190328_0919...Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/models/options.py", line 564, in get_field
    return self.fields_map[field_name]
KeyError: 'field_2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 150, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py", line 327, in remove_field
    self._remake_table(model, delete_field=field)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py", line 266, in _remake_table
    self.create_model(temp_model)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 300, in create_model
    columns = [model._meta.get_field(field).column for field in fields]
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 300, in <listcomp>
    columns = [model._meta.get_field(field).column for field in fields]
  File "/usr/local/lib/python3.7/site-packages/django/db/models/options.py", line 566, in get_field
    raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: RandomModel has no field named 'field_2'

Change History (1)

comment:1 Changed 4 years ago by Simon Charette

Resolution: duplicate
Status: newclosed

Duplicate of #28862 which will be fixed in Django 2.2 which is planned to be released in April 2019.

Note: See TracTickets for help on using tickets.
Back to Top