[odb-users] Create a foreign key constrain to a unique column of other table

Vassilios Kountouriotis b.kountouriotis at gmail.com
Wed Sep 13 14:05:29 EDT 2017

On Sep 13, 2017 8:33 PM, "Boris Kolpackov" <boris at codesynthesis.com> wrote:

Vassilios Kountouriotis <b.kountouriotis at gmail.com> writes:

> I'm mapping my existing database schema to ODB. That's why I have these
> weird issues...
> Furthermore, if this helps a bit, I'm really not interested in
> maintaining the loading capability from Log to the Ticket. All I'm
> interested in is creating the constraint that LogTicketID *must* be a
> value from Ticket.TicketID.

Well, if you are using the existing database schema then you already
have the constraint (ODB doesn't do any extra checking in this context
relying on the database to enforce constraints). Since you are not
interested in loading the pointed-to object (which, BTW, you can still
do with an object loading view if you change your mind), simply make
the member a string and be done with it:

std::string LogTicketID;


Yes, that would be my final "solution" but I would be giving up a part of
what makes ODB magical: its support for schema evolution... I would have to
constantly remember to handle these by hand if needed.

I'm migrating to ODB mostly for the code-first, single point declaration of
database schema and its associated ease of evolution...

Anyway, if it can't be done, it can't be done and I'll have to find a
workaround, probably along the lines you specified: declare it as a simple
string and run the SQL afterwards...

Might I suggest you consider such a feature for future releases? It would
be really nice if we could specify constraints with pragmas as we can with
the indexes...


More information about the odb-users mailing list