[odb-users] Loading of lazy ptr within odb object class

Lisa Fiedler lfiedler at informatik.uni-leipzig.de
Sat Feb 23 04:14:47 EST 2019


I am having trouble to execute the load operation on 
odb::lazy_shared_ptr within the odb class.

To identify the reasons I set up the following program:

Two odb classes: employee and employer (and some other classes but this 
is not really relevant).

Basically it looks like this:

#pragma db object pointer(std::shared_ptr) session table("schema1.employer")
class employer
   employer (const std::string& name);

   const std::string&
   name () const;
   // Employees of this employer.
   typedef std::vector<odb::lazy_weak_ptr<employee>> employees_type;

   const employees_type&
   employees () const;

   employees ();

   friend class odb::access;

   employer () {}

   #pragma db id type("VARCHAR (255)") column("name")
   std::string name_;

   #pragma db unordered table("schema1.employer_employees") 
id_column("name") value_column("id")
   employees_type employees_;

#pragma db object pointer(std::shared_ptr) session table("schema1.employee")
class employee

   typedef odb::lazy_shared_ptr<employer> employer_type;
   employee (const std::string& first,
             const std::string& last,
             std::shared_ptr<employer> employer)
       : first_ (first), last_ (last), emPloyer_ (employer)


   // Name.
   const std::string&
   first () const
     return first_;

   const std::string&
   last () const
     return last_;

   unsigned long& id(){
       return id_;

   // Employer.
   emPloyer () const
     return emPloyer_;

   emPloyer (employer_type employer)
     emPloyer_ = employer;

    // having issues here!!!
*  void printEmployer(){**
**      cout << emPloyer_.load()->name() << endl;**
**  }*

   friend class odb::access;

   employee () {}

   #pragma db id type("INTEGER") column("id")
   unsigned long id_;

   #pragma db type("VARCHAR (255)") column("first")
   std::string first_;

   #pragma db type("VARCHAR (255)") column("last")
   std::string last_;

   #pragma db not_null column("employer")
   employer_type emPloyer_;


Now what does not work out is the printEmployer method in the employee 
class (bold type setting). Error message:

"error: no match for ‘operator=’ (operand types are 
‘std::shared_ptr<employer>’ and 
‘odb::object_traits<employer>::pointer_type {aka employer*}’)
        p_ = i_.template load<T> (true); // Reset id"

If I execute this operation, i.e. emPloyer.load()->name(), for instance 
in the main method it executes just fine. I suspected the reason is, 
that in main.cpp I included employee-odb.hxx, which I did not do in 
employee.hxx where the above mentioned classes are defined. I therefore 
tried to include it there, but this apparently leads to some cyclic 

Thanks a lot for any suggestions to what I can do, to enable lazy ptr 
loading wihtin odb classes.

More information about the odb-users mailing list