python风暴查询引用集(一对多)

new9mtju  于 2021-06-25  发布在  Storm
关注(0)|答案(1)|浏览(324)

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

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)

我知道第一个是炼金术:

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

r9f1avp51#

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

相关问题