[odb-users] Store a tree in ODB

Boris Kolpackov boris at codesynthesis.com
Tue Nov 27 07:58:10 EST 2012

Hi Davide,

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

> However, my suggested solution will enforce a foreign key in the
> m_father relation, and I am not sure whether I can put a null value
> in there for the root of the tree (DBMS might refuse this value, am
> I right?).

You can store NULL in the pointer as long as you don't declare the
pointer not_null.

> I've slightly rethink this solution in this way:
> Class Node 
> {
> //...
> Private:
> std::vector< odb::boost::lazy_shared_ptr< Node > > m_children;
> };
> In this way I am actually generating a supplementary table that stores
> the tree structure, which I can navigate both ways to discover for each
> node either its father or its children. Does that sound like a more
> reasonable approach?

The additional table seems wasteful to me, especially since you can
achieve the same functionality without it:

#pragma db object
class Node 

  odb::boost::lazy_shared_ptr< Node > m_parent;

  #pragma db inverse(m_parent)
  std::vector< odb::boost::lazy_weak_ptr< Node > > m_children;


More information about the odb-users mailing list