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

时间:2017-03-23 13:51:03

标签: python ponyorm

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

其中一列/字段是持续时间(timedelta)。我正在尝试选择持续时间少于给定时间的所有行。

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

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

我做错了什么,或者有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

这是PonyORM本身的一个错误。我打开了一个问题here,它已在PR here中修复,应该在0.7.4版本中。

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