Skip to content

columnTransformer convert int to enum #3328

Answered by simolus3
robpot95 asked this question in Q&A
Discussion options

You must be logged in to vote

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 a columnTransformer 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 column myTable.myColumn, it could look like this:

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):

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@robpot95
Comment options

Answer selected by robpot95
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants