Realm schema validation error for embedded object

Hi, I have a Realm object with a list of embedded objects defined like so:

class InventoryItem: EmbeddedRealmObject {
    var upc: String = ""
    var quantity: Long = 0
    var quantityType: String = ""
    var retail: Long = 0
    var cost: Long = 0
    var totalRetail: Long = 0
    var totalCost: Long = 0
}
class InventoryBatch: RealmObject {
    @PrimaryKey
    var _id: ObjectId = ObjectId()
    var dateTime: RealmInstant = RealmInstant.now()
    var inventoryItems: RealmList<InventoryItem> = realmListOf()
    var inventoryType: String = ""
    var notes: String? = null
    var status: String? = null
    var store: String = ""
    var tenantId: String = ""
    var transferStore: String? = null
    var user: String = ""
}

According to the docs I believe this is how the schema should be defined. Here are how the models are defined in the docs:

// RealmList<E> can be any supported primitive
// or BSON type, a RealmObject, or an EmbeddedRealmObject
class Frog : RealmObject {
    var _id: ObjectId = ObjectId()
    var name: String = ""
    // List of RealmObject type (CANNOT be nullable)
    var favoritePonds: RealmList<Pond> = realmListOf()
    // List of EmbeddedRealmObject type (CANNOT be nullable)
    var favoriteForests: RealmList<EmbeddedForest> = realmListOf()
    // List of primitive type (can be nullable)
    var favoriteWeather: RealmList<String?> = realmListOf()
}
class Pond : RealmObject {
    var _id: ObjectId = ObjectId()
    var name: String = ""
}

// Implements `EmbeddedRealmObject` interface
class EmbeddedAddress : EmbeddedRealmObject {
    // CANNOT have primary key
    var street: String? = null
    var city: String? = null
    var state: String? = null
    var postalCode: String? = null
    var propertyOwner: Contact? = null
}

When I try to open a new realm like so:

                // Create a sync configuration for Inventory
                val inventoryConfig = SyncConfiguration.Builder(user, setOf(InventoryBatch::class))
                    .waitForInitialRemoteData(60.seconds)
                    .initialSubscriptions { realm ->
                        add(
                            realm.query<InventoryBatch>(),
                            "Inventory Batches"
                        )
                    }
                    .build()
                inventoryRealm = Realm.open(inventoryConfig)

I get the error: Schema validation failed due to the following errors:
- Property ‘InventoryBatch.inventoryItems’ of type ‘array’ has unknown object type ‘InventoryItem’

what am I missing here?

Hi, Zach.

Your syncConfig should include the embedded object type in the schema:

val inventoryConfig = SyncConfiguration.Builder(user, 
setOf(InventoryBatch::class, InventoryItem::class)) // include both classes
    .waitForInitialRemoteData(60.seconds)
    .initialSubscriptions { realm ->
        add(
            realm.query<InventoryBatch>(),
            "Inventory Batches"
        )
    }
    .build()
inventoryRealm = Realm.open(inventoryConfig)
1 Like

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