【问题标题】:Android Room Migration: Update Attribute Name By Creating New TableAndroid 房间迁移:通过创建新表来更新属性名称
【发布时间】:2018-10-07 05:10:34
【问题描述】:

问题

在我的项目中,我有一个名为“content”的房间桌,其 Double 属性为“archivedCount”。在最新版本的应用程序中,属性 archivedCount 属性被重命名为 dismissCount,仍然是类型 Double。

Android API 级别/SQL 版本

28 / 3.19

原创内容模型

@Entity(tableName = "content")
data class Content(@PrimaryKey var id: String, var archiveCount: Double) : Parcelable {...}

新的内容模型

@Entity(tableName = "content")
data class Content(@PrimaryKey var id: String, var dismissCount: Double) : Parcelable {...}

运行时错误

java.lang.IllegalStateException: Migration didn't properly handle content(app.coinverse.content.models.Content).

我检查了日志打印的 Expected 和 Found 表,它们看起来是相同的。

尝试的解决方案

我尝试使用 Google Developer Advocate 概述的 complex schema change 来修改一个属性/列的名称,但未成功。这是我尝试的基本版本。

val MIGRATION_1_2: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
    // Create the new table
    database.execSQL("CREATE TABLE content_new (id TEXT, dismissCount REAL, PRIMARY KEY(id))")
    // Copy the data
    database.execSQL("INSERT INTO content_new (id, dismissCount) SELECT id, archiveCount FROM content")
    // Remove the old table
    database.execSQL("DROP TABLE content")
    // Change the table name to the correct one
    database.execSQL("ALTER TABLE content_new RENAME TO content")
}

}

【问题讨论】:

    标签: android sql sqlite android-room


    【解决方案1】:

    看不出你的实现有什么问题,我建议你使用一个不名为 Content() 的不同 @Entity 类,然后再试一次。

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      相关资源
      最近更新 更多