[odb-users] Bidirectional Relationship Primary Key

Boris Kolpackov boris at codesynthesis.com
Fri Feb 13 05:26:27 EST 2015


Hi Chen,

Chen Weiguang <CWeiguan at dso.org.sg> writes:

> Currently I am able to generate the schema for X_Y relationship table
> (X_id, Y_id), but with no primary key when compiling with ODB compiler.
> 
> I would like the schema to have either a composite primary key, or
> unique index across the two fields when compiling with ODB compiler,
> so as to prevent multiple addition of same relationship. Is there a
> way to do this?

The only way to do this currently is by executing a piece of SQL that
adds the index (or the primary key) manually, after the schema creation.
I.e., ALTER TABLE ADD CONSTRAINT...

I've also added a TODO item to support this better. I think the clean
way would be the 'unique' pragma that tells ODB there are no duplicate
pointers:

#pragma db unique // No multiple pointers to the same object.
std::vector<std::shared_ptr<object>> objs_;

Then ODB can generate the primary key, which I believe is the idiomatic
way.

Boris



More information about the odb-users mailing list