[odb-users] Query only intermediate polymorphic objects

Boris Kolpackov boris at codesynthesis.com
Thu Nov 9 10:29:53 EST 2017

Dieter Govaerts <dieter.govaerts at bricsys.com> writes:

> Base (abstract)
>  |
>  +- DerivedA
>      |
>      +- DerivedB
> How can I query (with and without view) only the objects of type "DerivedA"?
> For example, I would like to query all ids of objects of type DerivedA but
> not of type DerivedB:
> #pragma db view object(DerivedA)
> struct DerivedAIdView
> {
>     unsigned id;
> };
> odb::result<DerivedAIdView> r(db()->query<DerivedAIdView>());

At the end of Section 8.2, "Polymorphism Inheritance" there is this

"Currently, the discriminator column is always called typeid and
 contains a namespace-qualified class name (for example, 'employee'
 or 'hr::employee')."

This is undocumented, but that column is also available in the
query object and is called 'typeid_'. You can use this information
to restrict the types of objects you are getting, for example:

using DerivedAIdQuery = odb::query<DerivedAIdView>

odb::result<DerivedAIdView> r(
    DerivedAIdQuery::typeid_ == "DerivedA"));


More information about the odb-users mailing list