Clement JACOB
Clement JACOB

Reputation: 1107

Python Storm - Query ReferenceSet (one to many)

I have 2 simple classes mapping an existing DB:

class File(object):
  __storm_table__ = 'files'
  fid = Int(primary=True)
  filename = Unicode()

class FileDownload(object):
  __storm_table__ = 'filefield_track'
  did = Int(primary=True)
  fid = Int()
  email = Unicode()
  date = DateTime()

  trackedfile = Reference(fid, File.fid)

File.filedownloads = ReferenceSet(File.fid, FileDownload.fid)

I just want to be able to find all File objects that have a non-empty File.filedownloads set. This can be done in python by just querying all File objects and filtering manually the File.filedownloads field, but I thought there was a cleaner way to do this (this doesn't work :) ):

store.find(File, File.filedownloads != None)
store.find(File, File.filedownloads.count() != 0)

I know the first one works in SQLAlchemy:

session.query(File).filter(File.filedownloads != None)

Upvotes: 3

Views: 448

Answers (1)

Clement JACOB
Clement JACOB

Reputation: 1107

I was able to find a 'dirty' workaround, which deal with internal IDs (fid)

# Find all files that have been downloaded
subselect = Select(FileDownload.fid, distinct=True)
for f in store.find(File, File.fid.is_in(subselect)):
    print(f.filename, f.filedownloads.count())

Upvotes: 1

Related Questions