1

In Django (3.2.8), I have a set of TextChoices, which I would like to change to IntegerChoices. I will keep the keys the same, just the values in the database stored will just differ. I do this for consistency and I'm fixing some legacy code. Naturally, I changed the TextChoices to IntegerChoices, and the corresponding field to an IntegerField. As expected however, upon migrating, I get a django.db.utils.DataError: invalid input syntax for type integer: "option1". I feel like there should be a way that I can define a mapping for this migration to happen, but wasn't able to find any documentation on this.

Niels Uitterdijk
  • 713
  • 9
  • 27
  • https://stackoverflow.com/questions/1117564/set-django-integerfield-by-choices-name this will lead to your answer – Tanveer Ahmad Jan 26 '22 at 09:35
  • Not in the slightest. I know how to set up TextChoices or IntegerChoices from scratch. However, I have a populated DB with TextChoices that I want to change into IntegerChoices. – Niels Uitterdijk Jan 26 '22 at 09:59
  • 2
    My suggestion would be to use an intermediate migration. In the first migration, you add `myfield_as_int`. You migrate all the data from your old column to this new column. Then, in a second migration, you remove the old field + rename the one you created in the first migration. More info: https://stackoverflow.com/questions/35999186/change-type-of-django-model-field-from-charfield-to-foreignkey – Bjorn Jan 26 '22 at 10:03
  • Thanks Bjorn! That's so straightforward, I'm questioning why I didn't think of that myself – Niels Uitterdijk Jan 26 '22 at 10:04

0 Answers0