#30297: When delete fields and unique together from model django fails on
migrations
--------------------------------------+------------------------
Reporter: ApaDoctor | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
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'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30297>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.085fc62f5c6bae277c65bdd0d20ea8d9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.