Str*_*yer 9 java android database-migration realm realm-migration
我需要添加@PrimaryKey两个由于白痴而缺少它的Realm模型.通过直接关系或RealmLists在多个其他模型中引用模型,两个模型中的一个也引用另一个模型.
我的第一个想法是在迁移中重命名模式并手动复制数据,但随后Realm抱怨模式在其他模式中链接,无法重命名.
这两个模式包含大约15000个可以压缩到大约100个的对象,它们完全相同并且由于缺失而被复制@PrimaryKey.
模型本身有点简单:
class ModelA extends RealmObject {
String primaryKey; // Is missing the @PrimaryKey annotation
String someField;
String someOtherField;
Date someDate;
ModelB relationToTheOtherProblematicModel;
}
class ModelB extends RealmObject {
String primaryKey; // Is also missing the @PrimaryKey annotation
// this class only contains String fields and one Date field
}
Run Code Online (Sandbox Code Playgroud)
当我添加@PrimaryKey到两个类的primaryKey字段时,如何迁移数据?
编辑澄清:
两个模式都包含多个完全相同的项目.
primaryKey | someField | someOtherField
------ | ------ | ------
A | foo | bar
A | foo | bar
A | foo | bar
A | foo | bar
B | bar | foo
B | bar | foo
B | bar | foo
C | far | boo
C | far | boo
C | far | boo
Run Code Online (Sandbox Code Playgroud)
由于primaryKey唯一标识它们,因此可以删除这些重复项.当我添加@PrimaryKey注释并进行迁移时,Realm显然会抱怨重复的值.我需要删除那些重复项而不破坏其他模型中的链接.
你有没有尝试过这样的事情:
RealmConfiguration config = new RealmConfiguration.Builder(this)
.schemaVersion(6) //the new schema version
.migration(new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
schema.get("ClassA").addPrimaryKey("primaryKey");
schema.get("ClassB").addPrimaryKey("primaryKey");
}
})
.build();
Realm.setDefaultConfiguration(config);
Run Code Online (Sandbox Code Playgroud)
编辑:我在此
基础上进行了编辑。以下步骤应该可以解决此问题:
1. 创建新字段,但不要将其标记为主键。
2. 使用转换将新字段设置为每个实例的唯一值
3. 向新字段添加索引。
4. 将新字段设为主键。
RealmConfiguration config = new RealmConfiguration.Builder(this)
.schemaVersion(6) //the new schema version
.migration(new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
schema.get("ClassA").addField("newKey", String.class)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.set("newKey", obj.getString("primaryKey"));
}
})
.addIndex("newKey")
.addPrimaryKey("newKey");
schema.get("ClassB").addField("newKey", String.class)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.set("newKey", obj.getString("primaryKey"));
}
})
.addIndex("newKey")
.addPrimaryKey("newKey");
}
})
.build();
Realm.setDefaultConfiguration(config);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
736 次 |
| 最近记录: |