[odb-users] One-to-many bidirectional relationship without intermediary table

Boris Kolpackov boris at codesynthesis.com
Wed Oct 16 10:55:12 EDT 2019

Mocnik Marko <marko.mocnik at lisec.com> writes:

> What am I doing wrong?

This has to do with the ordering of things due to circular relationship.
The composite id type of testi2 must be known before testi definition.
The below version compiles fine for me. You could also make it work (in
this case) by swapping the order of testi and testi2 definitions.

#include <vector>
#include <memory>
#include <string>

struct testi;
struct testi2;

#pragma db value
struct testi2_id_type
    testi2_id_type() {}
    testi2_id_type(int asdf_, int qwer_) : asdf(asdf_), qwer(qwer_) {}

#pragma db points_to(testi)
    int asdf;
    int qwer;

#pragma db object
struct testi
    testi () {}
    testi (int asdf, const std::string& desc) : asdf(asdf), description(desc) {}

#pragma db inverse(id.asdf)
    std::vector<std::shared_ptr<::testi2> > children;

#pragma db id
    int asdf;

    std::string description;


#pragma db object
struct testi2
    testi2() : parent(new testi()) {}
    testi2(int asdf, int qwer, const std::string &m) : parent(new testi(asdf, "")), qwer(qwer), moredata(m) {}

#pragma db transient
    std::shared_ptr<testi> parent;

#pragma db transient
    int qwer;

    std::string moredata;

#pragma db member(id) virtual(testi2_id_type) id column("") \
    get (id_type(this.parent->asdf, this.qwer)) \
    set (this.parent = std::shared_ptr<::testi>(new ::testi((?).asdf, "")); this.qwer = (?).qwer)


inline bool operator < (const testi2_id_type &l, const testi2_id_type &r)
    if (l.asdf != r.asdf)
        return l.asdf < r.asdf;
    return l.qwer < r.qwer;

More information about the odb-users mailing list