3

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

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)
4

1 回答 1

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())
于 2013-01-02T13:02:27.057 回答