Invalid property name RLMException on migration


#1

I’m getting an “Invalid property name for class” error during migration.

The gist of it is as follows:

  1. I start with a model, let’s call it Story.
  2. In a migration, I create a new model using migration.create. Let’s call this new model Book. Book has a field which contains a list of Story objects mentioned in 1.
  3. In a subsequent migration, I add a field to the Story model. Let’s call this new field “author”.

When I run a migration, I get a RLMException: “Invalid property name ‘author’ for class ‘Story’”. The RLMException happens in the migration in step 2 (in the migration.create call).

I’ve posted the issue here in more detail with code: https://stackoverflow.com/questions/56398431/realm-how-to-fix-invalid-property-name-rlmexception-during-migration

I don’t know if I fully understand the cause (much less how to fix it), but my guess is that when the Book model is created, Realm sees the ‘author’ field in the Story class, however, this field isn’t introduced until a subsequent migration.

Any help or insight into the issue would be greatly appreciated.


#2

I mentioned this in my response to your question but want to mention it here as well.

The question on SO is marked as mobile platform which may indicate your data is being stored in the cloud and using a sync’d realm; either Query or Full.

If that’s the case, here’s the important bit

You must not include a migration block

Either way though a couple of things to address

In a migration, I create a new model using migration.create

migration.create doesn’t create a model - it creates an object of the model class you specify. Perhaps that’s what you meant?

The other thing is the full migration block isn’t included. This is unclear

Book model is created, Realm sees the ‘author’ field in the Story class, however, this field isn’t introduced until a subsequent migration.

Your Book model isn’t included but if it’s keeping a reference to a List of Story Objects, and you want to add a property to the Story class, the Book will not be affected as it’s keeping a reference to the story, not a reference to the stories properties.

In other words if we have a PersonClass and a DogClass, and the PersonClass is

class PersonClass: Object {
    let dogs = List<DogClass>()
}

and a property is added the DogsClass, the PersonClass will be unaffected and a standard Migration block will automatically add that property to the DogClass.