主键约束已损坏。值已存在:0

时间:2015-05-11 08:05:19

标签: android realm

创建组织:

    Organization organization = realm.createObject(Organization.class); // Create a new object
    OrganizationId organizationId = realm.createObject(OrganizationId.class);
    organizationId.setAggregateId("1");
    organization.setOrganizationId(organizationId);
    organization.setOrganizationName("1-я Клиника Ташкентской Медицинской Академии");
    organization.setAddress("Адрес: г. Ташкент, ул. Фароби, 2");
    organization.setPhoneNumber("Тел.: (+99871) 214-51-01, 214-50-86, 214-50-43");
    organization.setKey(organization.getOrganizationName().toLowerCase());

当我在这个组织中添加新医生时,一切都会好起来的。应用程序正在运行,没有任何错误:

    Doctor doctor = realm.createObject(Doctor.class);
    //FULL NAME
    FullName fullName = realm.createObject(FullName.class);
    fullName.setFirstName("Joe");
    fullName.setLastName("Richard");
    fullName.setMiddleName("Brown");
    doctor.setFullName(fullName);
    //CONTACTS
    Contacts contacts = realm.createObject(Contacts.class);
    RealmList<PhoneNumber> phoneNumberRealmList = new RealmList<>();
    PhoneNumber pn = realm.createObject(PhoneNumber.class);
    pn.setPhoneNumber("+998903735173");
    phoneNumberRealmList.add(0, pn);
    contacts.setPhoneNumbers(phoneNumberRealmList);
    doctor.setContacts(contacts);
    //ORGANIZATION
    doctor.setOrganization(organization);
    //SPECIALIZATION
    RealmList<Specialization> specializationRealmList = new RealmList<>();
    Specialization specialization = realm.createObject(Specialization.class);
    specialization.setSpecializationName("Ревматолог");
    specializationRealmList.add(0, specialization);
    doctor.setSpecializationList(specializationRealmList);

当我添加第二位医生时,我收到错误(错误堆栈在下面):

Doctor doctor2 = realm.createObject(Doctor.class);//
//FULL NAME
FullName fullName2 = realm.createObject(FullName.class);
fullName2.setFirstName("Bob");
fullName2.setLastName("Richardson");
fullName2.setMiddleName("Brownowich");
doctor2.setFullName(fullName2);
//CONTACTS
Contacts contacts2 = realm.createObject(Contacts.class);
RealmList<PhoneNumber> phoneNumberRealmList2 = new RealmList<>();
PhoneNumber pn2 = realm.createObject(PhoneNumber.class);
pn2.setPhoneNumber("+998903720757");
phoneNumberRealmList2.add(1, pn2);
contacts2.setPhoneNumbers(phoneNumberRealmList2);
doctor2.setContacts(contacts2);
//ORGANIZATION
doctor2.setOrganization(organization);
//SPECIALIZATION
RealmList<Specialization> specializationRealmList2 = new RealmList<>();
Specialization specialization2 = realm.createObject(Specialization.class);
specialization2.setSpecializationName("Уролог");
specializationRealmList2.add(1, specialization2);
doctor2.setSpecializationList(specializationRealmList2);

错误实际上是这样的:

  

处理:uz.sunet.wm,PID:5505       java.lang.RuntimeException:无法启动活动ComponentInfo {uz.sunet.wm / uz.sunet.wm.activities.MainActivity}:   io.realm.exceptions.RealmException:主键约束已损坏。   值已存在:0               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)               在android.app.ActivityThread.access $ 800(ActivityThread.java:151)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)               在android.os.Handler.dispatchMessage(Handler.java:110)               在android.os.Looper.loop(Looper.java:193)               在android.app.ActivityThread.main(ActivityThread.java:5292)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:824)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)               at dalvik.system.NativeStart.main(Native Method)        引起:io.realm.exceptions.RealmException:主键约束已损坏。值已存在:0               at io.realm.internal.Table.throwDuplicatePrimaryKeyException(Table.java:731)               at io.realm.internal.Table.addEmptyRow(Table.java:380)               at io.realm.Realm.createObject(Realm.java:1037)               at uz.sunet.wm.da​​ta.MyRealm.initiateRealmData(MyRealm.java:88)               at uz.sunet.wm.activities.MainActivity.onCreate(MainActivity.java:60)               在android.app.Activity.performCreate(Activity.java:5264)               在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)   在android.app.ActivityThread.access $ 800(ActivityThread.java:151)   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)   在android.os.Handler.dispatchMessage(Handler.java:110)   在android.os.Looper.loop(Looper.java:193)   在android.app.ActivityThread.main(ActivityThread.java:5292)   at java.lang.reflect.Method.invokeNative(Native Method)   在java.lang.reflect.Method.invoke(Method.java:515)   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:824)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)   在dalvik.system.NativeStart.main(本地方法)

我不知道发生了什么。对于第一个医生我设置id - 0.第二 - 1.但是我得到了这个错误。我做错了什么?如何解决这个问题?

1 个答案:

答案 0 :(得分:9)

这是因为您将Realm.createObject()与具有主ID的模型类一起使用。使用createObject()时,会使用默认值创建新对象,包括。 0表示主键。如果您已经有一个主键为0的对象,则会抛出主键冲突错误,就像您的情况一样。

您有两种选择:

1)永远不要使用0作为对象的主键值,因为这会阻止进一步调用Realm.createObject()。这也意味着你不能连续两次调用这个方法。

2)使用独立对象,然后使用Realm.copyToRealm()Realm.copyToRealmOrUpdate()。这将使Realm能够在Realm中创建任何对象之前检查主值。

我建议使用后一种方法,因为它不易出错。