简体   繁体   English

Android Room迁移:通过创建新表来更新属性名称

[英]Android Room Migration: Update Attribute Name By Creating New Table

Issue 问题

In my project I a have a room table named ' content ' with a Double attribute ' archivedCount '. 在我的项目中,我有一个名为“ content ”的房间表,其双人间属性为“ archivedCount ”。 In the latest version of the app the attribute archivedCount attribute is re-named to dismissCount , still as type Double. 在该应用程序的最新版本中,属性archivedCount属性重命名为dismissCount ,仍为Double类型

Android API Level / SQL Version Android API级别/ SQL版本

28 / 3.19 28 / 3.19

Original Content model 原始内容模型

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

New Content model 新内容模型

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

Runtime error 运行时错误

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

I've inspected the Expected and Found tables the log prints and they appear to be identical. 我检查了日志打印的Expected and Found表,它们看起来是相同的。

Attempted Solution 尝试的解决方案

I attempted the complex schema change as outlined by a Google Developer Advocate unsuccessfully in order to modify the name of one attribute / column. 为了修改一个属性/列的名称,我尝试了Google Developer Advocate概述的复杂模式更改 ,但未成功。 Here is a basic version of what I attempted. 这是我尝试过的基本版本。

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")
}

} }

看不到您的实现有什么问题,我建议您使用另一个未命名为Content()的@Entity类,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM