[odb-users] Object reference in composite value type

Harald Frostel Harald.Frostel at jku.at
Mon Jan 27 04:16:11 EST 2014

Is there a way to use a shared_ptr/lazy_shared_ptr in a composite
In our database we use composite primary keys quite a lot. In most
cases we have collections of "child" objects that belong to a "parent"
object (1-n relationship). The child’s primary key consists of the
parent primary key and a child id that is unique within one parent.
Parent Table:
ID Values
1 …
2 …
Child Table:
ParentID ChildID Values
1  1  …
1  2  …
1  3  …
2  1  …
PRIMARY KEY("ParentID","ChildID"),
FOREIGN KEY ("ParentID") REFERENCES "ParentTable" ("ID")
The straight forward way for the child object would be to create a
composite value that consists of a reference to a parent object and a
child id, and use this as the id in the child object.
#pragma db value
struct ChildCompositePrimaryKey {
friend class odb::access;
#pragma db column("ParentID") not_null
std::shared_ptr<ParentObject> parent__;
#pragma db column("ChildID") not_null
int child_id_;
But when this is compiled with the odb compiler, I get an error:
error: object pointer member 'parent_' in a composite value type that
is used as an object id.
The workaround right now is to use custom set/get methods (replace the
reference in the composite value by an int parent_id_ and create a
lazy_pointer of this parent id during loading) for the child object id
but than, in the generate schema the foreign key relation is lost.
Is there a way to solve this differently (without changing the schema
of the database)?

Dipl.-Ing. Harald Frostel
Department of Computational Perception
Johannes Kepler University Linz
Altenberger Strasse 69
A-4040 Linz, Austria	    
Tel:  +43 732 2468 1521
Fax:  +43 732 2468 1520
Mail: harald.frostel at jku.at

More information about the odb-users mailing list