Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values are replaces by default value when updating #6129

Closed
N3TC4T opened this issue Sep 13, 2023 · 4 comments · Fixed by #6132
Closed

Values are replaces by default value when updating #6129

N3TC4T opened this issue Sep 13, 2023 · 4 comments · Fixed by #6132

Comments

@N3TC4T
Copy link

N3TC4T commented Sep 13, 2023

How frequently does the bug occur?

Always

Description

When you update a record and the schema includes default values, any properties that are not explicitly provided in the update will be replaced with their default values.

Note: This only happens after upgrading to v12

Schema

{
        name: 'MySchema',
        primaryKey: 'id',
        properties: {
            id: { type: 'int' },
            fieldOne: { type: 'string' },
            fieldTwo:  { type: 'string', default: 'DEFAULT_VALUE' },
        },
}

Creating the record

realm.create("MySchema", {id: 1337, fieldOne: "SOME_VALUE", fieldTwo: "NOT_DEFAULT_VALUE" })

Updating the record

realm.create("MySchema", {id: 1337, fieldOne: "SOME_OTHER_VALUE" },  Realm.UpdateMode.All)

This will cause the value of the fieldTwo``{{ field to be updated to }}``DEFAULT_VALUE, even though we didn't explicitly specify this property when performing the update.

from docs:

declare enum UpdateMode {
    /**
     * Objects are only created. If an existing object exists, an exception is thrown.
     */
    Never = "never",
    /**
     * If an existing object exists, only properties where the value has actually
     * changed will be updated. This improves notifications and server side
     * performance but also have implications for how changes across devices are
     * merged. For most use cases, the behavior will match the intuitive behavior
     * of how changes should be merged, but if updating an entire object is
     * considered an atomic operation, this mode should not be used.
     */
    Modified = "modified",
    /**
+     * If an existing object is found, all properties provided will be updated,
+     * any other properties will remain unchanged.     <==========
     */
    All = "all"
}

Expected behaviour

Default values should be used only when the property is missing, and this should happen exclusively when you're creating a new record that doesn't already exist.

Version

12.1.0

What services are you using?

Local Database only

Are you using encryption?

Yes

Platform OS and version(s)

iOS 16.4

@kneth
Copy link
Contributor

kneth commented Sep 13, 2023

@N3TC4T Thank you for reporting. For your details, I believe we can easily write a test to reproduce the bug. Out of curiosity (before writing the test), do you observe the same if you use Modified as mode?

@N3TC4T
Copy link
Author

N3TC4T commented Sep 13, 2023

Thanks @kneth, Yes that was the first thing I tried and It's the same problem with Realm.UpdateMode.Modified

@kneth
Copy link
Contributor

kneth commented Sep 14, 2023

@N3TC4T We have a fix in #6132, and we hope to do a release early next week.

@sync-by-unito
Copy link

sync-by-unito bot commented Sep 25, 2023

➤ kneth commented:

Released in v12.2.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants