【发布时间】: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