【问题标题】:How to migrate RealmJS db after change the column type?更改列类型后如何迁移 RealmJS db?
【发布时间】:2019-01-22 02:11:57
【问题描述】:

我在我的 react-native 应用程序中使用 realmjs DB,其中我有一个架构 settings。我想将该模式的属性类型从int 更改为string。所以我知道我需要执行迁移,我决定执行linear 迁移。对于迁移,我遵循了 Realm 文档中的示例,最终做了如下操作:

const schemaList = [schemaV1,schemaV2];
let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath);
while (nextSchemaIndex < schemaList.length) {
    const migratedRealm = new Realm(schemaList[nextSchemaIndex++]);
    migratedRealm.close();
}

export default new Realm(schemaList[schemaList.length - 1]);

schemaV1 是旧版本的数据库,schemaV2 是更改属性类型后的最新版本数据库。 schemaV2 还具有如下迁移功能:

    if (oldRealm.schemaVersion < 1) {
        const oldObjects = oldRealm.objects(TBL_MOBILE_SETTING);
        const newObjects = newRealm.objects(TBL_MOBILE_SETTING);
        for (let i = 0; i < oldObjects.length; i++) {
            newObjects[i].module    = oldObjects[i].module;
            newObjects[i].setting   = oldObjects[i].setting;
        }
    }

但最后,当我尝试运行该应用程序时,它会崩溃并显示一条错误消息

需要数据库迁移

这是否意味着schemaV2 中的迁移功能永远不会运行?如果是这样,如何确保所有迁移功能都正常运行?还是我错过了什么?

编辑

我发现 RealmJS 中不允许更改列类型,我添加了一个新列并尝试执行迁移,但仍然出现相同的错误。

【问题讨论】:

    标签: database react-native realm realm-mobile-platform realm-js


    【解决方案1】:

    问题出在我的猜测上。没有调用迁移函数。我必须创建一个export const 函数并在index 文件中调用迁移函数,因为它是在应用程序启动时调用的文件。

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2015-10-12
      • 2021-12-04
      • 2016-02-29
      • 2015-02-07
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多