我有一个模型ModelA
,它是两个RealmModules
的一部分。在一个RealmModule
中,我希望模型有一个主键,而另一个我不需要。所以我试图在Android应用程序启动时从模型中删除主键。并且
我是否曾提到在一个模块中,这个模型,ModelA
充当父模型,而在其他模块中,它的角色是儿童模型的子,即ModelC - > ModelB - > ModelA
以下是代码段:
public class ModelA extends RealmObject {
@PrimaryKey
private long id;
@Required
private String name;
//public getters and setters
}
而在其他RealmModule
我有ModelB和ModelC,它们是这样的:
public class ModelB extends RealmObject{
@PrimaryKey
private long id;
ModelA childModelA;
//public getters and setters
}
public class ModelC extends RealmObject{
@PrimaryKey
private long id;
ModelB childModelB;
//public getters and setters
}
模块本身看起来像:
@RealmModule(classes = {
ModelA.class
} , library = true)
public class SmallModule{}
@RealmModule(classes = {
ModelA.class,
ModelB.class,
ModelC.class,
} , library = true)
public class BigModule{}
当架构/模块为BigModule
时,我正试图从ModelA中删除PrimaryKey
final RealmObjectSchema objectSchema = realmSchema.get(ModelA.class.getSimpleName());
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
objectSchema.removePrimaryKey();
objectSchema.addIndex("id");
});
到目前为止一直很好..当我尝试在SmallModule
中复制/更新到域时,它的工作正常。但是一旦我尝试在BigModule
上做同样的事情,这就是我得到的:
04-10 21:49:17.002 12946-13321/org.jay.test1.debug E/REALM_JNI: jni: ThrowingException 2, columnIndex is less than 0., .
04-10 21:49:17.002 12946-13321/org.jay.test1.debug E/REALM_JNI: Exception has been throw: columnIndex is less than 0.
04-10 21:49:17.017 12946-12946/org.jay.test1.debug V/InputMethodManager: onWindowFocus: android.support.v4.widget.DrawerLayout{943b8b0 VFED..... .F....ID 0,0-480,800 #7f110086 app:id/drawer_layout} softInputMode=19 first=false flags=#81810100
04-10 21:49:17.017 12946-12946/org.jay.test1.debug V/InputMethodManager: START INPUT: android.support.v4.widget.DrawerLayout{943b8b0 VFED..... .F....ID 0,0-480,800 #7f110086 app:id/drawer_layout} ic=null tba=android.view.inputmethod.EditorInfo@c1bce40 controlFlags=#101
04-10 21:49:17.043 12946-12946/org.jay.test1.debug E/DBHelper: [main::1] Error while inserting record
04-10 21:49:17.045 12946-12946/org.jay.test1.debug E/DBHelper: [main::1] Subscriber Error::columnIndex is less than 0.
java.lang.ArrayIndexOutOfBoundsException: columnIndex is less than 0.
at io.realm.internal.Table.nativeFindFirstInt(Native Method)
at io.realm.internal.Table.findFirstLong(Table.java:1077)
at io.realm.ModelARealmProxy.copyOrUpdate(ModelARealmProxy.java:965)
at io.realm.ModelBRealmProxy.copy(ModelBRealmProxy.java:810)
at io.realm.ModelBRealmProxy.copyOrUpdate(ModelBRealmProxy.java:779)
at io.realm.ModelCRealmProxy.copy(ModelCRealmProxy.java:1183)
at io.realm.ModelCRealmProxy.copyOrUpdate(ModelCRealmProxy.java:1136)
at io.realm.BigModuleMediator.copyOrUpdate(BigModuleMediator.java:370)
at io.realm.Realm.copyOrUpdate(Realm.java:1505)
at io.realm.Realm.copyToRealmOrUpdate(Realm.java:953)
at org.jay.test1.DBHelper$1$1.execute(DBHelper.java:66)
at io.realm.Realm$1.run(Realm.java:1402)
at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
领域是2.3.1
答案 0 :(得分:0)
@Beender完全正确。当您更改架构时,许多内容(包括列的编号)都可能会发生变化。正如他所说:由于所有列索引都是在第一次打开类型域时被缓存的,因此后续的架构更改不会被反映出来。
我认为你不应该摆弄你的架构,试图将奇怪的数据塞入数据库。如果您有要存储的数据,即不主键,请在架构中为其创建一个位置,但不要尝试将其用作主键。