【问题标题】:Migrate embedded object to separate table. Room将嵌入对象迁移到单独的表。房间
【发布时间】:2020-05-26 15:40:19
【问题描述】:

CertificateElementEntity 实体有一个嵌入类 ImgData

我已将CertificateElementEntityImageData分成不同的表格。

但现在我不知道如何进行迁移。

open class CertificateElementEntity(
@IgnoreJson
@PrimaryKey
@ColumnInfo(name = "local_id")
var localId: Long? = null,
var data: String? = null,
var imageData: ImgData? = null) 

也许有人做了类似的迁移

【问题讨论】:

    标签: android sqlite android-room


    【解决方案1】:

    你可以试试这个通用的迁移模式(老实说,我没有经历过这样的迁移,所以可能有更简单的方法):

    1. 创建具有相同结构的临时表 [CertificateElementEntityTemp]。
    2. 将表 [CertificateElementEntity] 中的所有数据复制到 [CertificateElementEntityTemp]。
    3. 删除表 [CertificateElementEntity]。
    4. 创建表 [ImgData]。
    5. 使用新结构创建表 [CertificateElementEntity](仅使用 imageId 而不是嵌入表中的所有字段)。为 [imageId] 创建外键。
    6. 将所需数据从 [CertificateElementEntityTemp] 复制到 [ImgData]。
    7. 将所需数据从 [CertificateElementEntityTemp] 复制到 [CertificateElementEntity]。
    8. 删除表 [CertificateElementEntityTemp]。

    当然,所有这些你都应该用等效的 SQL 语句写在迁移部分。

    【讨论】:

    • @kalugin1912 那么,你尝试过这个还是不是你问的?
    猜你喜欢
    • 2018-02-20
    • 2020-09-02
    • 1970-01-01
    • 2018-10-26
    • 2018-01-06
    • 1970-01-01
    • 2019-08-26
    • 2020-05-17
    • 2019-02-13
    相关资源
    最近更新 更多