Adding fields to a model

I have developer mode enabled for my partitioned sync.
I added a few fields to my model in my Maui app and hoped these new fields would get synced back and appear in the collection in Atlas. The fields did not appear, and I tried various ways to force that to happen. I then tried terminated sync in Atlas and re-enabled it, but that did not help either. Eventually, something I did made the new fields appear.
Can anyone tell me the correct way to make this happen? My app is in its early stages, so I know I will be expanding various models as the app develops.
[Later]
I now see there are 8 documents in the collection in Atlas. 4 with the old schema and 4 with the new schema. It appears that the app is correctly showing the 4 docs with the new schema. It would be good to know what is the best approach to changing a schema. If there were thousands of docs in the collection, it would not be great to duplicate all of them each time the schema is changed.

Hi. Can you elaborate on what you did you change your schema? When you update your schema from dev mode or in the UI we will perform another initial sync to resync down all changes (and new fields in existing documents)

I just added new fields to the C# model in the MAUI app. It eventually worked, as I said, but I wondered if there was a proper procedure to do this to avoid clearing data on the device etc.

I have to say, when sync works, it is very impressive. I had 2 devices side by side with the same person logged in on each. Changing data on one screen was updated on the other screen in about 1 second or less. It looked quite magical. This is making use of MVVM so I was not having to tap save.

I have a similar problem now. I realised that one of my properties in the MAUI C# model had been set to required when it shouldn’t have been so I removed the attribute. Now, whatever I do I get an exception

“The following changes cannot be made in additive-only schema mode:\n- Property ‘User.name’ has been made optional.”

on this line

var realm = await Realm.GetInstanceAsync(syncConfig);

I terminated and restarted sync in App Services but that made no difference. I also uninstalled and reinstalled my mobile app (to clear data) but that made no difference.

The collection for the model that had the required attribute removed has yet to be synced back to Atlas. By that I mean the collection is empty in the database at the server.

I will try adding back the required attribute in the C# model to see if I can get it to work again. At present, when these kinds of problems happen, I don’t know how to fix them. In fact, I’ll try restarting Visual Studio because that has sometimes helped.

I’ve managed to get it to work again but the question remains, what is the correct procedure to change a model? I’m in dev mode making all schema changes in the MAUI app by changing the model. Currently, after any change, I have a long process of trial and error to make my mobile app work again. Deleting data for the app on the device, terminating sync and restarting, and deleting collections. I only try these things as a last resort to try to make it work again.

I am having the same issues, would you be so kind and explain the steps you take as I seem to be going around in circles when I make changes to the models in my MAUI app.

Thank you

When you modify your Realm model in the client-side of your app, the new fields will only be present in the objects that are created after the change. If you want the new fields to appear in the documents in the Atlas database, you need to perform a schema migration on the server-side to update the existing documents to the new schema.

The best approach to changing a schema would be to write a server-side migration script to update the existing documents. This script should update the existing documents in a way that is safe, efficient and handles any potential data loss or corruption. The script can be run as a single, one-time operation on the server, and it should be able to update all of the documents in the collection to the new schema. This way, you will not have to duplicate all of the documents each time the schema is changed, even if there are thousands of documents in the collection.

Hi, thanks @Sumanta_Mukhopadhyay for the excellent explanation. Changing schemas in sync can be difficult due to the fact that sync is history-based and therefore making a destructive change to the schema (changing the type of a field) requires invalidating history.

That being said, I believe the initial issue is about using developer mode. Currently, we only support making additive changes while in developer mode (adding tables and fields). It sounds like the initial issue what John was running into was that they were trying to change the optionality of a field (required to optional), which is currently a breaking change. Currently, the only procedure for a breaking change is to terminate sync, modify the schema, and re-enable sync (updating the schema directly in the UI with this change will terminate and re-enable sync on its own with a warning to the user) .

We have two projects on the horizon that could be of interest.

The first will be to allow for destructive changes to be made by clients while in dev mode. It is important to understand that this will still require sync to be terminated and re-enabled, but this will now be possible instead of getting the error above “field exists but is optional”.

The second project will be to allow breaking changes to occur in production without terminating and re-enabling sync by adding the concept of schema versioning. We are excited about this but we are still in the early stages of planning this feature.

Happy hacking,
Tyler

1 Like

Please let me know if can contribute to any of them