-
Hello, i would like to know how can i convert the index of enum to text of the enum. Before i stored the index of enum as int. Now i would like to transform the column to textEnum. I've seen example but they only use .cast which will not help in this case. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Good question, I should add this to the documentation. I didn't try this yet, but something like this should work: As you've mentioned, using final oldIntEnum = myTable.myColumn.dartCast<int>();
await m.alterTable(TableMigration(myTable, columnTransformer: {
myTable.myColumn: oldIntEnum.caseMatch(when: {
for (final value in MyEnum.values)
Variable(value.index): Variable(value.name),
}),
})); And if you haven't checked that out already, this is definitely something worth testing with data to make sure the mapping gets applied correctly. |
Beta Was this translation helpful? Give feedback.
Good question, I should add this to the documentation. I didn't try this yet, but something like this should work: As you've mentioned, using
alterTable
with acolumnTransformer
to make an altered column depend on values in the old column is the right approach. Here, the transformation will essentially map each allowed int value to the corresponding text value.If the enum class is called
MyEnum
and stored in the columnmyTable.myColumn
, it could look like this: