Last week I wrote this question regarding Backlinks in the DotNet-SDK Two-Way Relationship
Here the Code again just for reference:
public class Owner : RealmObject
{
[PrimaryKey, MapTo("id")]
public int ID { get; init; }
[MapTo("name"), Required]
public string Name { get; set; }
[MapTo("dogs"), Backlink(nameof(Dog.Owner))]
public IQueryable<Dog> Dogs { get; }
}
public class Dog : RealmObject
{
[PrimaryKey, MapTo("id")]
public int ID { get; init; }
[MapTo("name"), Required]
public string Name { get; set; }
[MapTo("owner")]
public Owner Owner { get; set; }
}
While the provided Code works in DotNet, we are having trouble to get the database-file to work properly in swift.
In dotnet I can access an Owner
from a Dog
and vice versa.
If we try to load the realm-file into swift using the documentation https://www.mongodb.com/docs/realm/sdk/swift/examples/define-a-realm-object-model/#define-an-inverse-relationship-property we cannot get the Backlink to work.
Either we receive an error:
ERROR Migration is required due to the following errors:
- Property 'Dog.Owner' has been removed
- Property 'Owner.Dogs' has been added
Or this error, if we try to open the database in read-only mode:
ERROR The following changed cannot be made in read-only schema mode:
- Property 'Owner.Dogs' has been added
Our Swift-Code looks like this:
class Owner: Object {
@Persisted(primaryKey: true) var id: Int
@Persisted(indexed: true) var name: String
@Persisted var Dogs: List<Dog>
}
class Dog: Object {
@Persisted(primaryKey: true) var id: Int
@Persisted(indexed: true) var name: String
@Persisted(originProperty: "Dogs") var owner: LinkingObjects<Owner>
}
static var config: Realm.Configuration = {
var migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
}
var config = Realm.Configuration(schemaVersion: 1, migrationBlock: migrationBlock)
config.objectTypes = [
Owner.self,
Dog.self]
config.fileURL = realmFile.url
config.readOnly = true
return config
}()
The provided SchemaVersion 1
is the same as provided, when the database is created in dotnet.
Is there something we are missing or are both sdks just not compatible in this case?
If any more code is needed to help me with my problem I will provide it.