Android Room: How to Migrate Column Renaming?

前端 未结 2 366
孤街浪徒
孤街浪徒 2021-01-11 14:42

Issue

My app is crashing because I am not handling migration properly. I\'m looking for a solution to migrate the name of 1 column in my table.

相关标签:
2条回答
  • 2021-01-11 14:59

    Solution

    Thanks to the guidance from @TimBiegeleisen we discovered that the Android implementation of SQLite 3.19 for API 27 and 28 has not yet upgraded to the version 3.25 SQLite which allows this feature outlined in this StackOverflow post.

    Once Android upgrades a command such as this to alter a table column will be possible: database.execSQL("ALTER TABLE content RENAME COLUMN archiveCount TO dismissCount")

    0 讨论(0)
  • 2021-01-11 15:17

    There is a solution without migration - use ColumnInfo:

    data class Content(@PrimaryKey var id: String,  @ColumnInfo(name = "archiveCount") var dismissCount: Double) : Parcelable{...}
    

    Database column will be still archiveCount, but in Kotlin property will be renamed.

    0 讨论(0)
提交回复
热议问题