[odb-users] Problem with bidirectional relationship

Aldo Laiseca alaiseca at gmail.com
Wed Jan 31 00:11:48 EST 2024


Hi, I have two tables that, for the sake of simplicity, could be seen as: 

CREATE TABLE IF NOT EXISTS TABLE_A(
	ID							         BIGSERIAL                 NOT NULL,
	… OTHER FIELDS ...
	PRIMARY KEY(ID)
);


CREATE TABLE IF NOT EXISTS TABLE_B(
	ID								VARCHAR(20) 		NOT NULL,
	ID_A								BIGINT			NOT NULL,
	… OTHER FIELDS ...
	PRIMARY KEY(ID),
	FOREIGN KEY(ID_A) REFERENCES TABLE_A(ID)
		ON DELETE NO ACTION
        	ON UPDATE NO ACTION
);


That is, I have a one to many relationship from A to B (A can have many B’s but B can have only one A). 

I have created the corresponding headers like this: 

#A.h

class B;

#pragma db object table(“TABLE_A”)
class A {
private:
    #pragma db get(getId) set(setId)
    #pragma db id auto column("id") not_null
    unsigned long id;

    #pragma db get(getB) set(setB)
    #pragma db value_null inverse(a)
    std::vector<odb::lazy_weak_ptr<B>> b;     /* This is line 45 in the real header which is causing the problem */

    …
public:
    A() = default;
    virtual ~A() = default;

    unsigned long getId() const { return id }
    void setId(unsigned long id) { this->id = id; }

    ….
};


# B.h

#pragma db object table(“TABLE_B")
class B {
private:
    #pragma db get(getId) set(setId)
    #pragma db id column("id") not_null
    std::string id;

    #pragma db get(getA) set(setA)
    #pragma db column("id_a") not_null
    odb::lazy_shared_ptr<A> a;

    …
public:
    B() = default;
    virtual ~B() = default;

    unsigned long getId() const { return id }
    void setId(unsigned long id) { this->id = id; }

    odb::lazy_shared_ptr<A> getA() const { return a; }
    void setA(odb::lazy_shared_ptr<A> a) { this->a = a; }

    ….
};

My odb scripts for these classes look like this:

odb -I /usr/local/include -d pgsql --generate-query --odb-epilogue "#include \”B.h\"" --std c++17 A.h
odb -I /usr/local/include -d pgsql --generate-query --std c++17 B.h

However, my script fails saying this: 

A.h:45:55: error: unable to map C++ type '::std::__new_allocator< ::odb::lazy_weak_ptr< ::B > >::value_type' used in data member ‘b' to a PostgreSQL database type
A.h:45:55: info: use '#pragma db value_type' to specify the database type
A.h:45:55: error: unable to map C++ type '::std::__new_allocator< ::odb::lazy_weak_ptr< ::B > >::value_type' used in data member ‘b' to a PostgreSQL database type
A.h:45:55: info: use '#pragma db value_type' to specify the database type

I have read the chapter 6 from the manual about bidirectional relationships and lazy pointers and based my code on the provided examples. Cannot figure out what the problem is.

As usual, your help is highly appreciated.

Regards,

Aldo



More information about the odb-users mailing list