AW: [odb-users] One-to-many bidirectional relationship without
intermediary table
Mocnik Marko
marko.mocnik at lisec.com
Tue Oct 15 05:56:57 EDT 2019
Okay, the generated SQL does look pretty good with the 'points_to' directive. (Took me some time to realize that this requires odb 2.5)
And I got the virtual data member mapping in testi2 to work like you sketched.
But how do I get now a bidirectional mapping?
Especially a vector of Testi2 s in Testi?
Also with a virtual data member? The docs did not help me on how to do that with a vector.
> I was able to get the identical mapping using these classes:
>
> #pragma db object
> struct testi
> {
> #pragma db id
> int asdf;
> };
>
> #pragma db object
> struct testi2
> {
> #pragma db value
> struct id_type
> {
> #pragma db points_to(testi)
> int asdf;
> int qwer;
> };
>
> #pragma db id column("")
> id_type id;
> };
>
> The generated database schema (PostgreSQL version):
>
> CREATE TABLE "testi" (
> "asdf" INTEGER NOT NULL PRIMARY KEY);
>
> CREATE TABLE "testi2" (
> "asdf" INTEGER NOT NULL,
> "qwer" INTEGER NOT NULL,
> PRIMARY KEY ("asdf",
> "qwer"),
> CONSTRAINT "asdf_fk"
> FOREIGN KEY ("asdf")
> REFERENCES "testi" ("asdf")
> INITIALLY DEFERRED);
>
> With virtual data members and a bit of effort you should be able to change testi2::id_type::asdf to a pointer, something along these lines (a sketch):
>
> #pragma db object
> struct testi2
> {
> #pragma db transient
> testi* asdf;
>
> #pragma db transient
> int qwer;
>
> #pragma db value
> struct id_type
> {
> #pragma db points_to(testi)
> int asdf;
> int qwer;
> };
>
> #pragma db member(id) virtual(id_type) id column("") \
> get (...) \
> set (...)
> };
More information about the odb-users
mailing list