【发布时间】:2022-01-22 12:56:24
【问题描述】:
我正在尝试对我的数据库执行迁移。
我要添加的表如下:
@Entity(tableName = "Recipe_Of_Day_Entity")
data class RecipeOfDayDTO(
@PrimaryKey
@ColumnInfo(name = "id")
val id : String = UUID.randomUUID().toString(),
@ColumnInfo(name = "vegetarian")
val vegetarian: Boolean,
@ColumnInfo(name = "vegan")
val vegan: Boolean,
@ColumnInfo(name = "glutenFree")
val glutenFree: Boolean,
@ColumnInfo(name = "dairyFree")
val dairyFree: Boolean,
@ColumnInfo(name = "veryHealthy")
val veryHealthy: Boolean,
@ColumnInfo(name = "image")
val image: String?,
@ColumnInfo(name = "imageType")
val imageType: String?,
@ColumnInfo(name = "instructions")
val instructions: String?)
迁移对象定义如下:
val MIGRATION_4_5: Migration = object : Migration(4, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
// https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo
database.execSQL("CREATE TABLE IF NOT EXISTS `RecipeOfDayDTO` (`id` TEXT NOT NULL, `vegetarian` " +
"INTEGER NOT NULL, `vegan` INTEGER NOT NULL, `glutenFree` INTEGER NOT NULL, `dairyFree` INTEGER NOT NULL," +
"`veryHealthy` INTEGER NOT NULL, `image` TEXT NOT NULL, `imageType` TEXT NOT NULL, `instructions` TEXT NOT NULL)")
}
}
我正在构建数据库如下:
@Database(entities = [IngredientDataClassDTO::class, RecipeNotificationClassDTO::class, RecipeOfDayDTO::class], version = 5, exportSchema = true)
abstract class IngredientDatabase : RoomDatabase() {
.
.
.
Room.databaseBuilder(
context.applicationContext,
IngredientDatabase::class.java,
"saved_ingredient_database"
)
// https://medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929
.addMigrations(MIGRATION_4_5)
.build()
我已确保Room Database Migration doesnt properly handle ALTER TABLE migration 中建议的预期和找到的表之间没有区别。事实上,我使用了文本检查器,唯一的区别是“PrimaryKeyPosition”,这在我刚才提到的链接中不是问题。
我不想破坏我以前的数据库,所以我避免使用 fallbackToDestructiveMigration()。无效和重新启动并没有消除错误。有没有其他人遇到过这个问题?
【问题讨论】:
-
我认为这个解决方案会帮助你github.com/orbitalsonic/…
标签: android kotlin android-room database-migration