[odb-users] Defining one-to-many bidirectional relationship

david bowe dpbowe at hotmail.com
Thu Feb 9 05:53:45 EST 2017


Hi there,


I have been trying to define a one to many relationship with the attached code. However, when defining the relationship pointers I get the below error asking me to define a database type. I can do this and the code will compile but it creates a relationship through a supplier_order table which is not what I need. I need each order to reference one suppler


database.hxx:111:46: error: unable to map C++ type '::__gnu_cxx::new_allocator< ::boost::weak_ptr< ::order > >::this_type' used in data member 'orders_' to a PostgreSQL database type

database.hxx:111:46: info: use '#pragma db value_type' to specify the database type


Could anybody help me with what I am doing wrong?


Thanks,


David
-------------- next part --------------
// database.hxx
//

#ifndef DATABASE_HXX
#define DATABASE_HXX

#include <string>
#include <vector>
#include <odb/core.hxx>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

// Forward declaration
class order;

#pragma db object table("suppliers")
class supplier
{
public:
  typedef ::order orders_type;

  supplier (const std::string& title,
            const std::string& phone,
            const std::string& address_line_1,
            const std::string& address_line_2,
            const std::string& address_line_3,
            const std::string& address_line_4,
            const std::string& address_line_5)
    : title_ (title), phone_ (phone), 
      address_line_1_ (address_line_1), address_line_2_ (address_line_2),
      address_line_3_ (address_line_3), address_line_4_ (address_line_4),
      address_line_5_ (address_line_5)
  {}

  unsigned long id() const {
    return id_;
  }
  const std::string& title() const {
    return title_;
  }
  const std::string& phone() const {
    return phone_;
  }
  const std::string& address_line_1() const {
    return address_line_1_;
  }
  const std::string& address_line_2() const {
    return address_line_2_;
  }
  const std::string& address_line_3() const {
    return address_line_3_;
  }
  const std::string& address_line_4() const {
    return address_line_4_;
  }
  const std::string& address_line_5() const {
    return address_line_5_;
  }

  void title(const std::string& title) {
    title_ = title;
  }
  void phone(const std::string& phone) {
    phone_ = phone;
  }
  void address_line_1(const std::string& address_line_1) {
    address_line_1_ = address_line_1;
  }
  void address_line_2(const std::string& address_line_2) {
    address_line_2_ = address_line_2;
  }
  void address_line_3(const std::string& address_line_3) {
    address_line_3_ = address_line_3;
  }
  void address_line_4(const std::string& address_line_4) {
    address_line_4_ = address_line_4;
  }
  void address_line_5(const std::string& address_line_5) {
    address_line_5_ = address_line_5;
  }

private:
  friend class odb::access;

  supplier() {}

  #pragma db id auto
  unsigned long id_;

  #pragma db type("VARCHAR(255)") column("title")
  std::string title_;
  #pragma db type("VARCHAR(255)") column("phone") null
  std::string phone_;
  #pragma db type("VARCHAR(255)") column("address_line_1")
  std::string address_line_1_;
  #pragma db type("VARCHAR(255)") column("address_line_2") null
  std::string address_line_2_;
  #pragma db type("VARCHAR(255)") column("address_line_3") null
  std::string address_line_3_;
  #pragma db type("VARCHAR(255)") column("address_line_4") null
  std::string address_line_4_;
  #pragma db type("VARCHAR(255)") column("address_line_5") null
  std::string address_line_5_;
  #pragma db type("TIMESTAMP") column("created_at") options("DEFAULT CURRENT_TIMESTAMP")
  boost::posix_time::ptime created_;
  #pragma db type("TIMESTAMP") column("updated_at") options("DEFAULT CURRENT_TIMESTAMP")
  boost::posix_time::ptime updated_;

  #pragma db value_not_null inverse(supplier_) 
  std::vector<boost::weak_ptr<orders_type> > orders_;
};

#pragma db object table("orders")
class order
{
public:
  typedef ::supplier supplier_type;

  order (boost::shared_ptr<supplier_type> supplier,
         std::string bd_code)

    : supplier_ (supplier), bd_code_ (bd_code)
  {}

  unsigned long id() const {
    return id_;
  }
  const std::string& bd_code() const {
    return bd_code_;
  }

  void bd_code(const std::string& bd_code) {
    bd_code_ = bd_code;
  }

public:
  // Belongs to: supplier
  const boost::shared_ptr<supplier_type>& supplier() const {
    return supplier_;
  }

  void supplier(const boost::shared_ptr<supplier_type>& supplier) {
    supplier_ = supplier;
  }

private:
  friend class odb::access;

  order() {}

  #pragma db id auto
  unsigned long id_;

  #pragma db not_null type("VARCHAR(255)") column("code")
  std::string bd_code_;
  #pragma db type("TIMESTAMP") column("created_at") options("DEFAULT CURRENT_TIMESTAMP")
  boost::posix_time::ptime created_;
  #pragma db type("TIMESTAMP") column("updated_at") options("DEFAULT CURRENT_TIMESTAMP")
  boost::posix_time::ptime updated_;

  #pragma db not_null
  boost::shared_ptr<supplier_type> supplier_;

};

#endif // DATABASE_HXX



More information about the odb-users mailing list