[odb-users] Query object A by range of objects B, with 1:n relation

Markus Klemm markus at markusklemm.net
Thu Sep 3 09:32:33 EDT 2015


I have a problem using the odb query language with a one to many relationship.
Suppose I have a persistent class file and directory. These have a bidirection one-to-many relationship. To query a set of files with criteria of one directory I successfully used the directory member of the query<file> class. But how do I use it with a range of directories, i.e. all files from this set of directories.

To get more concrete, said I have the definitions:

#pragma db object table("directories") session pointer(std::shared_ptr<directory>)
struct directory{
#pragma db column("directory_id") type("uniqueidentifier") id not_null
	boost::uuids::uuid directory_id;
#pragma db inverse(directory)
	std::vector<std::shared_ptr<file>> files;}};

#pragma db object table("files") session pointer(std::shared_ptr<file>)
struct file{
#pragma db column("file_id") type("uniqueidentifier") id not_null
	boost::uuids::uuid file_id;
#pragma db column("directory_id") not_null
	std::weak_ptr<directory> directory;};

So for one directory the query is obvious: query<file>(query<file>::directory == query<file>::_val(directory_instance)).
But the two solutions I've come to have 2 different problems: 
Solution 1: 
set<decltype(directory::directory_id)> directories;
odb::query<file>::directory->directory_id.in_range(directories.cbegin(),directories.cend());
Fails with a sql exception, "wrong syntax near directory_id"
Solution 2:
std::set<directory> directories
odb::query<file>::directory.in_range(directories.cbegin(),directories.cend());
Can't compile because there is, I assume, no overload of in_range() for directory. (Error is 'odb::mssql::val_bind<T>::val_bind' : none of the 2 overloads could convert all the argument types)

Can I solve this with the odb query language?

Thanks in advance and

best regards/Mit freundlichen Grüßen

Markus Klemm



More information about the odb-users mailing list