【问题标题】:How to change indices in a room migration?如何更改房间迁移中的索引?
【发布时间】:2018-02-18 16:00:27
【问题描述】:

旧实体如下:

@Entity(tableName = "d_course",
    foreignKeys = @ForeignKey(entity = DUser.class, parentColumns = "id", childColumns = "studio"),
    indices = @Index(value = "studio"))

新实体如下:

@Entity(tableName = "d_course",
    foreignKeys = @ForeignKey(entity = DUser.class, parentColumns = "id", childColumns = "studio"),
    indices = @Index(value = {"id", "studio"}))

如何迁移此索引更改。

【问题讨论】:

  • 仅供参考.....在这里我想补充一些 ROOM 期望的重要内容。每当您添加新列并为其编写迁移时。在 MODEL 类的其他字段的末尾添加此列。 (是的!在模型类中)房间订单很重要。以及每当您创建索引时。始终遵循约定,即 否则房间不会让你添加索引。

标签: android migration android-room


【解决方案1】:

尝试在迁移中指定这个索引变化,例如(Kotlin):

val MIGRATION_1_2 = object : Migration(1, 2) {
    override fun migrate(database : SupportSQLiteDatabase) {
        ...
        database.execSQL("CREATE INDEX index_d_course_id_studio ON  d_course(id, studio)")
    }
}

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2020-05-17
    • 2019-02-13
    • 2018-02-20
    相关资源
    最近更新 更多