迁移中如何使用 React Native 在 Realm DB 中生成 uuid?

问题描述 投票:0回答:0

我目前正在尝试更改我的一些模式的主键。他们曾经有一个 int

id
字段,但现在我想实现
uuid
.

我的代码如下:

export const realmParams = {
    path: 'db.realm',
    migration: (oldRealm: Realm, newRealm: Realm) => {
        if(oldRealm.schemaVersion <= 34) {
            const allFatherObjects = newRealm.objects('FATHER')
            for (const fatherObject of allFatherObjects) {
                const fatherUuid = new Realm.BSON.UUID()
                fatherObject.uuid = fatherUuid

                const allSonObjectsFromFather = newRealm.objects('SON')
                .filtered(`id_father = ${fatherObject.id_father}`)

                for (let i = 0; i < allSonObjectsFromFather.length; i++) {
                    const sonObject = allSonObjectsFromFather[i]

                    sonObject.uuid = new Realm.BSON.UUID()
                    sonObject.father_uuid = fatherUuid
                }
            }
        }
    },
    schema: [
        FatherSchema,
        SonSchema,
    ],
    schemaVersion: 35,
}

但它总是返回错误

Exception in HostFunction: Primary key property 'Son.uuid' has duplicate values after migration.

我尝试登录迁移,但什么也没有出现。

此外,我丢弃了两个模式中的主键属性以查看传递的内容,并且在查看 realm studio 时,我用 uuid 填写的每个字段都有一个空字符串

我尝试使用来自 npm 的 uuid 库的 uuid.v4(我已经将 getRandomValues 导入到我的索引中)并登录到其他类,我可以确认它有效并且它显示了一个 uuid。 迁移后一切正常。我能够正常生成一个新的 uuid 并将其保存在表中,所以我认为问题就在迁移函数内部。但是我不想在没有主键的情况下保留我的模式。

在迁移时生成 uuid 有什么问题,我该如何解决?

编辑:在领域版本 10 中,它运行良好。更新一些库后出现错误,其中之一是领域。目前在 11.9.0。不知道有没有关系

react-native realm uuid
© www.soinside.com 2019 - 2024. All rights reserved.