Python Storm - 查询引用集(一对多)

时间:2013-01-02 10:04:14

标签: python mysql apache-storm

我有两个映射现有数据库的简单类:

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)

我只是希望能够找到具有非空File集的所有File.filedownloads个对象。这可以在python中完成,只需查询所有File个对象并手动过滤File.filedownloads字段,但我认为有一种更简洁的方法可以做到这一点(这不起作用:)):

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

我知道第一个在SQLAlchemy中工作:

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

1 个答案:

答案 0 :(得分:1)

我能够找到一个“脏”的解决方法,它处理内部ID(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())