[odb-users] Inverse side of std::map with object pointer as key

Per Edin per.edin at sequence-point.se
Sat Oct 26 20:58:35 EDT 2019


Hi!

I'm having some trouble getting an inverse pragma to work for the
following code snippet, see the comment NOT LOADED.

Locations in item is always empty when I load an item from the database.

Am I missing some options? After digging around and tracing SQL I
found that ODB tries to load item::locations by looking at the value
column in locations_items table, i.e. the value of the std::map. I
need it to look at the key of the map. Is this possible somehow?

Any help is much appreciated.

Kind regards,
Per Edin

#ifndef schema_hxx_
#define schema_hxx_

#include <application/types.hxx>

namespace application::world {

// Item
//

class location;

class item : public std::enable_shared_from_this< item > {
  size_t id;
  string ext_id;
  string name;

  vector< weak_ptr< location > > locations; // NOT LOADED
};

#pragma db object(item) table("items") session
#pragma db member(item::id) column("id") id auto
#pragma db member(item::ext_id) column("ext_id") unique not_null
#pragma db member(item::name) column("name") unique not_null
#pragma db member(item::locations) inverse(items)

// Location
//

class location : public std::enable_shared_from_this< location > {
  size_t id;
  string ext_id;
  string name;

  map< shared_ptr< item >, long > items; // loads fine
};

#pragma db object(location) table("locations") sessions
#pragma db member(location::id) column("id") id auto
#pragma db member(location::ext_id) column("ext_id") unique not_null
#pragma db member(location::name) column("name") unique not_null
#pragma db member(location::items) column("items") value_not_null

}

#endif



More information about the odb-users mailing list