Reputation: 629
I want to add two tables without loosing any data from old tables. So i have created table "Emotion"
public class Emotion extends RealmObject {
private String uuid;
private String name;
and EmotionValue
public class EmotionValue extends RealmObject {
private String uuid;
private String emotionId;
private String name;
My RealmMigrations
has this code
public class RealmMigrations implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
final RealmSchema schema = realm.getSchema();
...
if(oldVersion == 5) {
schema.create("Emotion")
.addField("uuid", String.class)
.addField("name", String.class);
schema.create("EmotionValue")
.addField("uuid", String.class)
.addField("emotionId", String.class)
.addField("name", String.class);
oldVersion++;
}
and my App
has
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(getApplicationContext());
RealmConfiguration config = getRealmConfig("superdatabase");
Realm.setDefaultConfiguration(config);
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())
.build());
}
public static RealmConfiguration getRealmConfig(String name) {
return new RealmConfiguration.Builder()
.name(name)
.schemaVersion(5)
.migration(new RealmMigrations())
.build();
}
}
when i run an app i has an error:
Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Class 'Emotion' has been added.
- Class 'EmotionValue' has been added.
at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
at io.realm.internal.OsSharedRealm.<init>(OsSharedRealm.java:171)
at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:241)
at io.realm.BaseRealm.<init>(BaseRealm.java:135)
at io.realm.BaseRealm.<init>(BaseRealm.java:103)
at io.realm.Realm.<init>(Realm.java:163)
at io.realm.Realm.createInstance(Realm.java:499)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:341)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:284)
at io.realm.Realm.getInstance(Realm.java:428)
at com.timecontrolapp.models.App.onCreate(App.java:17)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4553)
at com.timecontrolapp.models.App.onCreate(App.java:17) has this line Realm.setDefaultConfiguration(config);
. My question is, how i can add some tables to my exist database without loosing any data from old database?
Upvotes: 1
Views: 317
Reputation: 4470
So you need just to upgrade schemaVersion() as well, which is in your case 6 instead of 5.
Upvotes: 1