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

huangapple go评论64阅读模式
英文:

How to generate UUID in Realm DB using React Native inside migration?

问题

在迁移函数中生成UUID后,主键属性'Son.uuid'出现重复值的异常。
英文:

I'm currently trying to change the primary key for some of my schemas. They used to have an int id field but now I want to implement UUID.

Here's my code:

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,
}

But it always returns this error:

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

I tried to log inside the migration, but nothing appears.

Also, I throw away the primary key property in both schemas to see what was passed and when looking on realm studio every field I fill in with UUID has an empty string.

I've tried to use uuid.v4 of UUID library from npm (I've already imported the getRandomValues into my index) and logging into other classes, I can confirm that it works and it shows me a UUID.

After the migration everything works fine. I am able to generate a new UUID normally and save it in the table, so I assume the problem is just inside the migration function. But I don't want to keep my schemas without a primary key.

What is the problem with generating a UUID on migration and how can I solve it?

In realm version 10, it worked fine. I got the error after updating some libs and one of them is realm. Currently in 11.9.0.

答案1

得分: 0

由于某种原因,领域配置对象中的字段 migration 更改为 onMigration。这就解释了为什么无法看到任何日志,或者断点不会停在函数内部的代码。领域根本没有执行迁移。这也解释了在从模式中删除 uuid 后为什么我创建的所有字段都为空。

他们可能忘记更新文档了。

新代码:

export const realmParams = {
    path: 'db.realm',
    onMigration: (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,
}
英文:

For some reason, the field migration from realm config object changed to onMigration. That explain why no logs could be saw, or breakpoints don't stop the code inside the function. Realm simply wasn't executing the migration. Also explain why all fields that I created was blank after removing uuid from the schemas.

They probably forgot of update the docs.

The new code:

export const realmParams = {
    path: &#39;db.realm&#39;,
    onMigration: (oldRealm: Realm, newRealm: Realm) =&gt; {
        if(oldRealm.schemaVersion &lt;= 34) {
            const allFatherObjects = newRealm.objects(&#39;FATHER&#39;)
            for (const fatherObject of allFatherObjects) {
                const fatherUuid = new Realm.BSON.UUID()
                fatherObject.uuid = fatherUuid

                const allSonObjectsFromFather = newRealm.objects(&#39;SON&#39;)
                .filtered(`id_father = ${fatherObject.id_father}`)

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

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

huangapple
  • 本文由 发表于 2023年5月17日 20:18:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定