Data lost by a migration for renaming a field

153 views
Skip to first unread message

chen

unread,
Sep 8, 2018, 12:21:54 PM9/8/18
to Django users
Hi,

I have a model which contains a BooleanField. I changed its name to a new one and updated help text as well. django generates operations RemoveField and AddField  in sequence for those changes. As a result of running this migration, original data is lost. I was confused by this behavior why django does not generate an operation to just rename the field by executing ALTER TABLE. Renaming a database field name is a common task during development, I think django should handle this properly without developer's extra work to migrate data (but probably SQLite could be an exception).

I actually did nothing special to make that migration. django version is 1.11.13 and database backend is mariadb.

Any idea about this issue?

Thanks.

Regards,
Chenxiong Qi

Jason

unread,
Sep 8, 2018, 12:48:52 PM9/8/18
to Django users
https://docs.djangoproject.com/en/1.11/ref/migration-operations/#alterfield

There are AlterField and RenameField operations available with migrations, but you probably have to include them in your custom migrations.  Reason being, how would django know that you're just renaming a field and not deleting it and adding a new field in its place?  In other words, how would django know going from 

text = models.TextField()
to
other_name = models.TextField()

is a rename operation instead of remove and add new column?

chen

unread,
Sep 9, 2018, 2:40:13 AM9/9/18
to Django users
Hi Jason,

Thanks for explaining. It makes sense. I'm also thinking it might be much helpful to detect the rename as much as possible and give warning message about developer has to modify the migration to rename field properly. Alternatively, instead of detecting potential rename by django framework itself, it should be also helpful to add an option to makemigrations to do a rename migration, e.g.

    ./manage makemigrations --rename-field app

This new option would be useful for the case of creating a dedicated migration just to rename a field.

Jason

unread,
Sep 9, 2018, 9:56:37 AM9/9/18
to Django users
I'm also thinking it might be much helpful to detect the rename as much as possible and give warning message about developer has to modify the migration to rename field properly

sure, but how would you do this? And how would you cover all the possible cases to detect whether the dev intends to delete a field and replace it with another vs renaming the field entirely?  For the django core devs, I can see this being a very large effort feature that, despite everything, is very brittle and introduces uncertanity in migrations.

Having an option like that for makemigrations could be useful as an alternative.  Perhaps you can write up a ticket for that at https://code.djangoproject.com/query for discussion?
Reply all
Reply to author
Forward
0 new messages