[odb-users] Relational tables with an association table

Boris Kolpackov boris at codesynthesis.com
Thu Jul 18 13:34:52 EDT 2013


Hi Adam,

Adam Walters <adamw at animetix.com> writes:

> What I am having a hard time understanding is why is this additional 
> table required as it appears to be used to perform the join between
> the PC and the CT tables?

Because that's the relationship you specified. You basically have
a unidirectional to-many relationship (every PC object can point
to multiple CT objects).

You definitely need to read up on inverse relationships since I
believe that the solution to your problem. If you turn this into
a bidirectional relationship (at the tables level any relationship
is always bidirectional), then you can choose which side actually
results in a table/column. And in your case you can choose to make
the CT pointer the straight (non-inverse) side to avoid the extra
table. Try this and see what schema you get:

class PC
{
  #pragma db inverse (_pc)
  std::vector< std::unique_ptr<CT> > _ct;
};

class CT
{
  std::unique_ptr<PC> _pc;
};


> In my case, I presume the id field would contain the id of a PC
> record and the value field would contain the same value,

No, the value will contain the id of the CT row.


> If I have to create a separate table for each 1-to-many relationship
> i define, does the ODB insert mechanism populate these for me 
> automatically?

Yes, of course.

Boris



More information about the odb-users mailing list