[odb-users] Store a tree in ODB

Boris Kolpackov boris at codesynthesis.com
Mon Nov 26 11:00:53 EST 2012


Hi Davide,

Davide Anastasia <Davide.Anastasia at qualitycapital.com> writes:

> I want to store a tree inside a database table: every tuple can be the
> father of several other tuples (in a sort of tree-shaped data
> structure).
> 
> Would something like this work?
>
> class Node 
> {
>   //...
> private:
>   odb:: boost::lazy_shared_ptr< Node> m_father;
> }

Yes, there is no technical reason why this won't work. Self-references
are supported by ODB. How practical this will be is another question,
though. Every "navigation" from child to parent or the other way around
will require a database query. If that's too slow then a database-
specific data type (e.g., ltree in PostgreSQL) or a custom binary
representation of a tree (stored as BLOB) might be the alternatives.


> Can also a child relationship be realized in this way?

Yes, though you would need to use a weak pointer for one of the
references. And probably make one of them inverse (it is a
bidirectional relationship).

Boris



More information about the odb-users mailing list