Reputation: 142
I'm want to add new field in my model
public class NoteModel extends RealmObject {
@PrimaryKey
private int id; //new field
private String note;
private String header;
private Date date;
/*
* few getters and setters
*/
}
And I don't want to make any migrations so
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
//if I'm right it's delete all data from db and change it's schema (that's OK for me)
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build()
}
}
But I still got exception io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors: - Primary Key for class 'NoteModel' has been added. - Property 'NoteModel.id' has been added.
It's happens in onCreate method (MainActivity.java).
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
realm = Realm.getDefaultInstance(); //error right here
What should I actually do? Get information from here: "realm migration needed", exception in android while retrieving values from realm db
Upvotes: 1
Views: 1120
Reputation: 81539
You need to call Realm.setDefaultConfiguration(config)
after creating said config.
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build()
Realm.setDefaultConfiguration(config); // <---
}
}
Upvotes: 2