realm-java: Primary key not defined for field 'id' in existing Realm file. Add @PrimaryKey.
Hello! I upgraded from 0.88.2 to 0.90.1 and refactored my RealmObject classes and renamed them. Everything works fine if I clear the app data and it’s a new, clean Realm, but migrating from the old version doesn’t work.
Stacktrace:
io.realm.exceptions.RealmMigrationNeededException: Primary key not defined for field ‘id’ in existing Realm file. Add @PrimaryKey. at io.realm.ChannelRealmProxy.validateTable(ChannelRealmProxy.java:154) at io.realm.DefaultRealmModuleMediator.validateTable(DefaultRealmModuleMediator.java:71) at io.realm.Realm.initializeRealm(Realm.java:316) at io.realm.Realm.createAndValidate(Realm.java:284) at io.realm.Realm.createInstance(Realm.java:263) at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:118) at io.realm.Realm.getDefaultInstance(Realm.java:195) …
Channel.java:
public class Channel extends RealmObject {
@PrimaryKey
private long id;
private String name;
//...
DbMigration.java:
//Don't want to create a Channel schema, if the previously named one exists
if(schema.get("TvObject") == null && schema.get("Channel") == null) {
schema.create("Channel")
.addField("id", long.class, FieldAttribute.PRIMARY_KEY)
//redacted, just a bunch of String, int and boolean fields
}
//...
if(oldVersion == 7) {
schema.rename("TvObject", "Channel");
schema.get("Channel")
//redacted the changes, it's a bunch of field renaming and removing
oldVersion++;
}
It also happens with two other classes that I renamed, and it’s not always the same one that causes the crash. It changes when I redo the upgrade from the old app version to the new one.
I’ve tried using Long with @Required
instead of long, adding the primary key in the migration (which causes an exception), nothing helps. Am I doing something wrong?
P.S. Is there a better way to structure migrations with regards to creating the objects? If I rename the objects again, I’ll have three == null
checks in the if()s.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (10 by maintainers)
@edgars-supe
The issue has been identified and fixed. Once our next release is deployed, there won’t be the issues you’ve experienced. Hope this helps your development cycle. I’m closing the issue for now.