Atlas App Service Generating a schema to match Realm Object Models problem

I currently have an iOS app that connects to my database thru Atlas Sync. When I created the database, I named all of my collections starting with lowercase. Obviously the object models within my iOS app should start with a capital letters. I am running into a problem with the schema generated and syncing to the client iOS app.

Currently I have a Condition object model class.

class Condition: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId?
    @Persisted var __v: Int?
    @Persisted var condition_string: String?
}

I have the following JSON schema generated by the Atlas Sync App.

{
  "title": "condition",
  "properties": {
    "__v": {
      "bsonType": "int"
    },
    "_id": {
      "bsonType": "objectId"
    },
    "condition_string": {
      "bsonType": "string"
    }
  }
}

I can only get my app to sync if I rename my class Condition to lower case ie:

class condition: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId?
    @Persisted var __v: Int?
    @Persisted var condition_string: String?
}

How can I get a successful sync to occur without renaming my collections to start with a capital letter? I assume this is what is causing my problem when i get the “Client Query is invalid/malformed error” and the Xcode " ERROR "Invalid query (IDENT, QUERY): failed to parse query: query contains table not in schema: “condition”

Well, I found the problem to be not handling the realm migration. In order to get the app functioning again, I needed to delete the realm instance and start over.

Still trying to figure out how to implement migration within the app!

Hi @Chris_Stromberg let me first understand your issue.
Do you already have an App in production which has on the schema the following object

class Condition: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId?
    @Persisted var __v: Int?
    @Persisted var condition_string: String?
}

and you created a Device Sync App with the following schema

{
  "title": "condition",
  "properties": {
    "__v": {
      "bsonType": "int"
    },
    "_id": {
      "bsonType": "objectId"
    },
    "condition_string": {
      "bsonType": "string"
    }
  }
}

which cannot be changed.
Now you are getting an error because the model name on your App doesn’t match the name on the server schema.
So what you have to do is migrate the model above to use a lowercase capital letter so you can match it with Device Sync?
Let me know if this your issue? So I can help you with the migration.

Hello Diana,

Thanks for the reply. You are correct, I made breaking changes to my app when the server schema did not match the model name. To get around this I have been locating the associated realm file on my iMac and deleted it. When a new realm is instantiated, with the matching server schema and model name, it works as it should. I am currently researching how to perform migrations when making breaking changes. For now, I would be fine with using “deleteRealmIfMigrationNeeded”, but I can’t figure out how to implement this while configuring a “flexibleSyncConfiguration” for a user.

Hi @Chris_Stromberg you cannot set deleteRealmIfMigrationNeeded you will get this error `Cannot set ‘deleteRealmIfMigrationNeeded’ when sync is enabled (‘syncConfig’ is set).

Synced realms do not have schema versions and automatically migrate objects to the latest schema. Synced realms only support non-breaking schema changes, as it mentions in the documentation.

1 Like

If you need to migrate some local data to a synced Realm, you can do the migration on the local realm, and then use our writeCopy API to pass the migrated data to a synced Realm. Let me know if this is helpful or you need more help.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.