Query LinkingObject depends on Null or not. Shows error.
Goal
Query LinkingObject by the size of a list.
Expected Results
I have two models, one is Group and another one is Contact. In the group, I have a RealmList containing contact. In the contact, I have a LinkingObject pointing back to Group. I want to query all contacts without groups.
Actual Results
I tried to query the contacts which has null groups. However, it shows error, " LinkingObject from field job is not nullable."
Code Sample
Group Model
public class Group extends RealmObject {
@PrimaryKey
private String groupTitle;
@Index
private Integer id;
@Index
private String followupTime;
@Index
private Date scheduleDate;
@Index
private Integer followupIntentId;
@Index
private Integer scheduleIntentId;
private RealmList<Contact> contacts;
private String numUsers;
private Integer imageButtonId;
private Boolean isBatchGroup = false;
private Boolean isBirthdayGroup = false;
private Boolean isFollowupHideIndustry;
private Boolean isScheduleHideIndustry;
private Integer numFollowupContacts;
private Integer numScheduleContacts;
private Integer followupThresholdDays;
private Integer scheduleThresholdHours;
private Integer scheduleType = -1;
private Integer serviceScheduleStatus = STOPPED;
private Integer serviceFollowupStatus = STOPPED;
private Date lastScheduleSentDate;
private Date lastFollowupSentDate;
private Date lastCheckedDate;
private Integer totalCheckedNum;
}
Contact
public class Contact extends RealmObject {
@Index
private Integer id;
@Index
private Date lastMsgDate;
@Index
private Boolean isMessageArchived;
@Index
private String lastMessage;
private String firstName;
private String lastName;
private String job;
private String title;
private String city;
private String state;
private String birthday; // mm/dd
private String description;
private String address;
private Integer relationStatus = 0;
private Integer gender = 0;
private Integer birthdayYear = 0;
private Date createdDate;
private Date lastInBoundMsgDate;
private Date lastOutBoundMsgDate;
private Integer numOfGuests;
private Boolean isReferral;
private Boolean isLastSynced;
private Boolean isInIndustry;
private Boolean isHighQuality;
//TODO: fix order
private Integer order;
private String facebookUrl;
private String websiteUrl;
private String instagramUrl;
private String twitterUrl;
private String linkedInUrl;
private String imgUrl;
private Boolean imgDownloaded = false;
private RealmList<ContactPhone> phones;
private RealmList<Email> emails;
@LinkingObjects("contacts")
private final RealmResults<Group> groupList = null;
private RealmList<Interest> interests;
private RealmList<Category> categories;
RealmResults<Contact> contacts = mRealm.where(Contact.class).isNull("groupList").findAll();
Version of Realm and tooling
Realm version(s): 3.4.0
Realm sync feature enabled: yes
Android Studio version: 2.1.2
Which Android version and device: ?
This is most likely fixed in 3.5.0 see https://github.com/realm/realm-java/issues/4856
Hello @cchuang6 Can you upgrade your Realm Java to 3.5.0?
It works. Thanks
On Jul 19, 2017 22:42, "LYK" [email protected] wrote:
Hello @cchuang6 https://github.com/cchuang6 Can you upgrade your Realm Java to 3.5.0?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/realm/realm-java/issues/4996#issuecomment-316602612, or mute the thread https://github.com/notifications/unsubscribe-auth/AAkmAcAL9OVQMnl95oWsW0zO1w1OL2E-ks5sPujTgaJpZM4OdNzu .
Good to hear it works. I'm closing this issue.
Tried the same on Realm "5.5.0" and it crashes,
So, I cloned the project, mentioned in https://github.com/realm/realm-java/issues/4856 and added a linking object to Dog class mentioned there and tried a similar query mention with this issue, which is:
Root object -> check isNull on the Linked object (direct object mentioned and also present in the schema) -> find all
which as mentioned in #4856 is:
RealmQuery<Person> query = realm.where(Person.class).isNull("dog");
and this works,
But what is mentioned in this issue (#4996) which is the exact reverse of that mentioned in #4856 which can also be written as:
Linked Object -> check isNull on a back linked object (present as a
RealmResultsvariable and also not in the schema) -> findAll
which, after adding it to the project, looks something like this:
RealmQuery<Dog> query = realm.where(Dog.class).isNull("linkedPerson");
and it does not work.
Exception:
java.lang.IllegalArgumentException: Illegal Argument: LinkingObject from field dog is not nullable.
The final model looks something like:
public class Dog extends RealmObject {
private Long ate_at;
public Long getAte_at() {
return ate_at;
}
public void setAte_at(Long ate_at) {
this.ate_at = ate_at;
}
@LinkingObjects("dog")
public final RealmResults<Person> linkedPerson= null;
}
Which is exactly similar to:
RealmResults<Contact> contacts = mRealm.where(Contact.class).isNull("groupList").findAll(); mentioned above.
Thus summarizing, isNull query does not work for backlinked objects (one with annotations), but was marked with #4856 which is for normal linked objects (one without annotations, which works flawlessly)
Tried the same on realm version 3.5.0 and it crashes to an older version as well.
I can push the link of the test project as well if needed.