使用 timedeltas 查询小马 orm 的断言错误

Assertion error querying pony orm using timedeltas

我有一个 sqlite3 数据库,我正在使用 Pony orm(版本 0.7.1,python 3.6.0)访问。

其中一个 columns/fields 是持续时间 (timedelta)。我正在尝试 select 所有持续时间小于给定时间的行。

我正在做的最简单的例子:

import pony.orm                                                                    
from datetime import timedelta                                                     

db = pony.orm.Database()                                                           


class TestTime(db.Entity):                                                         
    id = pony.orm.PrimaryKey(int, auto=True)                                       
    td = pony.orm.Required(timedelta)                                              


db.bind('sqlite', ':memory:', create_db=True)                                      
db.generate_mapping(create_tables=True)                                            

if __name__ == "__main__":                                                         
    with pony.orm.db_session:                                                      
        tt1 = TestTime(td=timedelta(seconds=12))                                   
        tt2 = TestTime(td=timedelta(seconds=18))                                   
        db.commit()                                                                

    delta = timedelta(seconds=15)                                                  
    with pony.orm.db_session:                                                      
        TestTime.select(lambda x: x.td < delta) 

当我 运行 这个我得到 AssertionError: delta

使用pony.orm.select(x for x in TestTime if x.td < delta)语法查询同样报错

如果有帮助,完整的跟踪是:

Traceback (most recent call last):
  File "ponytd.py", line 23, in <module>
    TestTime.select(lambda x: x.td < delta)
  File "<string>", line 2, in select
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/utils/utils.py", line 58, in cut_traceback
    return func(*args, **kwargs)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/core.py", line 3596, in select
    return entity._query_from_args_(args, kwargs=None, frame_depth=3)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/core.py", line 3943, in _query_from_args_
    return Query(code_key, inner_expr, globals, locals, cells)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/core.py", line 5087, in __init__
    translator = translator_cls(tree, extractors, vartypes, left_join=left_join)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 269, in __init__
    translator.dispatch(if_)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 82, in dispatch
    return ASTTranslator.dispatch(translator, node)  # default route
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/asttranslation.py", line 29, in dispatch
    translator.dispatch(child)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 82, in dispatch
    return ASTTranslator.dispatch(translator, node)  # default route
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/asttranslation.py", line 24, in dispatch
    stop = translator.call(pre_method, node)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 135, in call
    reraise(exc_class, exc, tb)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/utils/utils.py", line 85, in reraise
    try: raise exc.with_traceback(tb)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 125, in call
    try: monad = method(node)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 649, in preCompare
    translator.dispatch(right)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 83, in dispatch
    translator.call(translator.dispatch_external, node)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 135, in call
    reraise(exc_class, exc, tb)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/utils/utils.py", line 85, in reraise
    try: raise exc.with_traceback(tb)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 125, in call
    try: monad = method(node)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 119, in dispatch_external
    monad = translator.ParamMonad.new(translator, t, (varkey, None, None))
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 1646, in new
    result = cls(translator, type, paramkey)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 1654, in __init__
    Monad.__init__(monad, translator, type)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 1006, in __init__
    monad.mixin_init()
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 986, in wrapper
    return method(monad, *args, **kwargs)
  File "/home/user/.local/share/virtualenvs/test-Tqz3IJV7/lib/python3.6/site-packages/pony/orm/sqltranslation.py", line 1310, in mixin_init
    assert monad.type is time
AssertionError: delta

我做错了什么,还是有更好的方法?

这是 PonyORM 本身的一个错误。我开了个issue here, it was fixed in a PR here 应该是0.7.4版本发布的

遗憾的是,它没有及时合并到 0.7.3 版本中。