Android RealmObjectSchema.removePrimaryKey和columnIndex小于0
我有一个模型Android RealmObjectSchema.removePrimaryKey和columnIndex小于0,android,realm,Android,Realm,我有一个模型ModelA,它是两个RealmModules的一部分。在一个RealmModule中,我希望模型有一个主键,而另一个没有。所以我试图在android应用程序启动时从模型中删除主键。我做到了 我有没有提到,在一个模块中,这个模型,即ModelA充当父模型,在另一个模块中,它的角色是子模型的子模型,即ModelC->ModelB->ModelA 以下是代码片段: public class ModelA extends RealmObject { @PrimaryKey
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{}
这就是当schema/module为BigModule
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
中复制/更新到realm时,它工作正常。但只要我尝试在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@Beender完全正确。当您更改模式时,很多事情都会发生变化,包括列的编号。正如他所说:由于所有的列索引都是在类型化领域第一次打开时缓存的,因此后续的模式更改不会被反映出来
我认为你不应该摆弄你的模式,试图把奇怪的数据塞进数据库。如果您有要存储的数据,但该数据不是主键,请在架构中为其留出一个位置,但不要尝试将其用作主键。您肯定应该在此处提交一个问题,并提供一个复制该问题的最小示例。您似乎正在从迁移块中更改架构?如果你这样做,肯定会出问题。因为所有列索引都是在类型化领域第一次打开时缓存的,所以模式更改将不再反映到类型化领域。我可以问一下为什么需要这样做吗?@beender有两个模式,其中只有一个需要主键。我需要在ordedr中从一个模式复制到另一个模式以维护历史记录。因此,如果存在Primarykey,并且我插入了更新,那么它将覆盖以前的记录,这超出了维护历史记录的目的。如果我尝试插入,它会抛出异常。是的,这是在迁移块外完成的。PrmaryKey删除是在创建MainActivity时执行的。@Beendar还有其他方法来实现吗?因此,如果我理解正确,您将
id
定义为对象中的PK,但是由于您的数据来自JSON,可能有重复的值,因此您不能将其用作PK,所以您希望删除它,对吗?为什么不从对象定义中删除@PrimaryKey
?如果您仍然需要两者,我建议您在对象中同时定义intpkid
和intid
。然后根据json值计算pkId
,使其唯一。