I can't seem to be able to find anything about this (in library or in docs) though I can't believe no one raised this issue before. So if it's a duplicate - my pardon.
I have a Pin class, that has Session session field. Both Types extend RealmObject.
I need to find all pins that are from particular session, but I can't do realm.where(Pin.class).equalsTo("session", session).findAll(); query.
Is such query possible with realm?
That query should work (apart from equalTo instead of equalsTo). In what way does it fail? Does it not find what you expect?
There's just no suchMethod - equalTo(String fieldName, T extends RealmObject) there:


P. S.: realm version: compile 'io.realm:realm-android:0.77.0'
Oh right. We don't allow querying for equality on RealmObjects yet. Does your session object have an identifying field you can use instead?
Well, no, but I already have a workaround in mind. It's just that I was sure that there is such way of querying, even thought we've already used that in our project before. Now it's clear.
P. S.: maybe it would be good to explicitly add that to the Docs section on realm.io site? Like this:

I was surprised this issue haven't been raised before here (or maybe I just used wrong search query).
Anyway - thanks for your help and for a great library!
Yeah, this looks mostly like an oversigt on our parts and something we should add support for.
@cmelchior how is a relationship saved inside db (I mean, what type is it? Subtable? Mixed?)? I want to contribute :)
Hi @ISherlockHolmes
That would be great.
References to other RealmObjects are _Links_. You can see reference to them in both the Row (https://github.com/realm/realm-java/blob/master/realm/src/main/java/io/realm/internal/Row.java) and Table (https://github.com/realm/realm-java/blob/master/realm/src/main/java/io/realm/internal/Table.java) classes. RealmLists are similar saved in LinkLists.
I am hoping that the querying mechanism would include a way to query for objects which do not have a relationship. (#969)
RealmResults
@vvictor10 You're right - it could be a useful feature to query for objects with or without a relationship.
@ISherlockHolmes
I might have been a bit hasty. Implementing this functionality properly require the use of some functions in the core database as it already supports this use case, so asking you to look at it would probably be a bit unfair. On the positive side that probably means it is pretty fast to implement.
Hi,
i'm in a similar situation as @Den-Rimus's but in my case the _Session_ realm object does have an identifying field.
@emanuelez wrote:
Oh right. We don't allow querying for equality on RealmObjects yet. Does your session object have an identifying field you can use instead?"
Is there a way to retrieve all my _Pin_ objects write away using only realm queries or do i have to retrieve my _Pins_ first and then keep only those that their _Session_'s id is the one that i want?
Thank you in advance for answering
@le0nidas It is a bit hard to answer your question when I cannot see your actual data model. But it sounds like you should try to refactor your model using RealmList so you can query it.
Realm isn't a relational database (or an ORM on top of one), but more an object database (http://en.wikipedia.org/wiki/Object_database). You might wish to think in other terms.
@le0nidas I will give you an example for a many-to-one relationship querying. I have a Model 'Account' and a 'Transaction' one. The second one has: _private Account account;_
To query all items of a specific account I do:
_RealmResults
where accountNumber is a field in 'Account'(in this case also the primary)
Tell me if that helped.
@kneth and @sideris thank you both for your answers. The _"account.accountNumber"_ is what i was looking for. I guess the next time i must read more carefully the documents!
I close the issue as it seems that @sideris solved your problem (thanks @sideris)
This issue shouldn't be closed; @sideris helped @le0nidas with an unrelated problem. @Den-Rimus's original issue is that there's no way to use a RealmObject as the value of an equalTo call, and that functionality is still missing (and it would be _immensely_ useful).
Primary keys aren't necessary in many cases because objects can link directly to other objects without referencing properties, but you're forced to generate an ID if you want to refer to an object in an equalTo call. Using the example from the docs, There's no way to do this:
Dog fluffy = realm.where(Dogs.class)
.equalTo("name", "Fluffy")
.findFirst();
// ... later ...
RealmResults<Person> results = realm.where(Persons.class)
.equalTo("dogs", fluffy)
.findAll();
@cmelchior suggested that it was a valid improvement. Can this be reopened?
Additionally #969 was closed as a duplicate of this issue; it's another use case of the same feature and it's impossible to do currently.
@6bangs I have created #3259 (using your comment above) to have a cleaner feature request.
Thanks!
Most helpful comment
@le0nidas I will give you an example for a many-to-one relationship querying. I have a Model 'Account' and a 'Transaction' one. The second one has: _private Account account;_
To query all items of a specific account I do: result = realm.where(Transaction.class).equalTo("account.accountNumber", 1234567890).findAll();_
_RealmResults
where accountNumber is a field in 'Account'(in this case also the primary)
Tell me if that helped.