[odb-users] Query on many-to-one for "contains an object with property X"

Boris Kolpackov boris at codesynthesis.com
Wed Nov 8 11:28:10 EST 2017

Lukas Barth <lists at tinloaf.de> writes:

> #pragma db object
> class B
> {
>   int value;
>   #pragma db not_null
>   shared_ptr<A> a;
>> };
> #pragma db object
> class A
> {
> #pragma db inverse(a)
>  std::vector<std::shared_ptr<B>> bs;
>> };
> I now want to query for all As that have a B with a value of 42 associated
> with them. I can't find the syntax for that (if that's possible) in the
> documentation. I would assume that I can access "bs" as as a field of A, but
> then I would need a "contains" operator and something to build the nested
> query, something along the lines of this:
> db.query<A> (query::bs->contains( query<B>(query::value == 42) ))

Yes, using containers in query conditions is not yet supported (though
it's on a TODO list). The way you can achieve this now is with an object
loading view. I.e., join the two objects in a view, load A, and in
the query condition use B::value. Plus you will probably want the
distinct clause. The manual has more information and some examples.


More information about the odb-users mailing list