Data lost by a migration for renaming a field

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Data lost by a migration for renaming a field

tkdchen
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/847a9c72-7b7b-4eca-9624-989340ee82f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Data lost by a migration for renaming a field

Jason Johns
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?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8015df46-1146-4491-b788-e2cb291835c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Data lost by a migration for renaming a field

tkdchen


On Sunday, September 9, 2018 at 12:48:52 AM UTC+8, Jason wrote:
<a href="https://docs.djangoproject.com/en/1.11/ref/migration-operations/#alterfield" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F1.11%2Fref%2Fmigration-operations%2F%23alterfield\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFkayD949tKeTehN9LPIbXconlbbg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F1.11%2Fref%2Fmigration-operations%2F%23alterfield\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFkayD949tKeTehN9LPIbXconlbbg&#39;;return true;">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?

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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9103e0a3-1e2f-47a3-ba66-fd390cfee643%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Data lost by a migration for renaming a field

Jason Johns
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?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3ae0aee2-adc9-4a4d-ada9-3177c9e3d66f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.