Seeing a SCHEMA_VALIDATION_FAILED for this, but why?

I have a couple of simple RealmObject classes that look like this:

type or paste code here
class Patient : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var firstName: String = ""
var lastName: String = ""
var visits: RealmList<PelletInsertionVisit> = realmListOf(PelletInsertionVisit())
var lastFourOfSSN: String = ""
}

class PelletInsertionVisit : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var visitDate = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)
var pelletsUsed: RealmList<Pellet> = realmListOf(Pellet())
}

class Pellet: EmbeddedRealmObject {
var size = PelletSize.ZERO.value
var lotNumber: String = "UNKNOWN"
}

When I perform this code, the open throws an exception:

fun openPatientRealm() : Realm {
val configuration = RealmConfiguration.create(schema = setOf(Patient::class))
Logger.d("About to open Realm Configuration: $configuration")

try {
    val realm = Realm.open(configuration)  //EXCEPTION HERE

    Logger.d("Realm opened: : ${realm.toString()}")

    return realm
}
catch (ex: Exception) {
    Logger.e("Exception caught: " + ex.message)
    Logger.e("Stack trace: " + ex.stackTraceToString())
}

throw Exception("Realm Could Not be Opened")
}

The exception is:

[RLM_ERR_SCHEMA_VALIDATION_FAILED]: Schema validation failed due to the following errors:

  • Property ‘PelletInsertionVisit.pelletsUsed’ of type ‘array’ has unknown object type ‘Pellet’

What is it that I am doing wrong, please? It looks, to me, to be like the documentation says it should be using the EmbeddedRealmObject but I’m missing something.

Hi Darrin,

You need to include the embedded class in the schema. Same for the PelletInsertionVisit class if you’re using it in the realm – the schema should contain all of the classes that you want to work with in your realm.

// Update the schema to include the embedded object class
val configuration = RealmConfiguration.create(schema = setOf(Patient::class, Pellet::class))

This is mentioned in the Open a Realm page, but I also have a ticket open to improve the docs here since it seems to be cause for confusion :sweat_smile:

BINGO!!!

Thank you so much for this and thank you for getting the documentation updated to make this more clear!

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