#28438: Initial migration creates fields not listed in the migration if mixin
class
changes
-------------------------------------+-------------------------------------
Reporter: Michal Dabski | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
migration,models,mixin,postgresql |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by giannisterzopoulos):
This seems to be still an issue. Here's couple of things that I am
noticing:
- There is, of course, no problem if you run `makemigrations` once in the
end, after converting the Mixin into an abstract Model.
- There is also no problem if you run `migrate` before changing the Mixin,
and then `makemigrations` and `migrate` again.\\
That actually results to the exact same migration as in the problematic
case.
Here is a test I wrote that can reproduce the issue, in case it can be
helpful:
{{{
# <In tests.migrations.test_operations.OperationTests>
def test_field_can_be_added_to_mixin_bases(self):
# Replicating the scenario where a Mixin class has initially no fields
# and does not extend Model; and then gets refactored to an abstract
Model
# with fields specified.
class SomeMixin(models.Model):
cuteness = models.IntegerField(null=True)
class Meta:
abstract = True
operation = migrations.CreateModel(
name='CutePony',
fields=[
('id', models.AutoField(primary_key=True)),
('pink', models.IntegerField(default=3)),
],
bases=(SomeMixin, models.Model),
)
app_label = 'test_28438'
project_state, new_state = self.make_test_state(app_label, operation)
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state,
new_state)
project_state = new_state
new_state = project_state.clone()
operation = migrations.AddField(
model_name='CutePony',
name='cuteness',
field=models.IntegerField(null=True)
)
operation.state_forwards(app_label, new_state)
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state,
new_state)
self.assertTableExists(f'{app_label}_cutepony')
}}}
(This raises: `django.db.utils.OperationalError: duplicate column name:
cuteness`)
--
Ticket URL: <https://code.djangoproject.com/ticket/28438#comment:11>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018b2470918c-b9f70ab8-9f4f-4821-ba17-5eda569c4633-000000%40eu-central-1.amazonses.com.