我想在我的模型中添加新字段
public class NoteModel extends RealmObject {
@PrimaryKey
private int id; //new field
private String note;
private String header;
private Date date;
/*
* few getters and setters
*/
}
我不想进行任何迁移
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()
}
}
但我仍然遇到异常io.realm.exceptions.RealmMigrationNeededException:由于以下错误,需要迁移: - 已添加类'NoteModel'的主键。 - 已添加属性'NoteModel.id'。
在onCreate方法(MainActivity.java)中发生。
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
realm = Realm.getDefaultInstance(); //error right here
我该怎么做? 从这里获取信息:"realm migration needed", exception in android while retrieving values from realm db
答案 0 :(得分:1)
创建所述配置后,您需要致电Realm.setDefaultConfiguration(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); // <---
}
}