我目前正在使用Python用Chalice开发适用于AWS的API,该API使用Pony ORM处理我们的数据库。尝试使用类似db.select(s.start_time for s in db.Session)
的选择查询时,出现“期望的字符串或类似字节的对象”错误(下面是完整的堆栈跟踪)。但是,使用像这样的db.Session.select(lambda s: s.id = 3)
的lambda进行查询就像预期的那样。我不知所措,可能是生成时不喜欢db.Provider部分,但是我不确定Pony期望什么。 pdb,但是我不确定它告诉我什么。
堆栈跟踪:
回溯(最近通话最近): 文件“ c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ chalice \ app.py”,行842,位于_get_view_function_response中 响应= view_function(** function_args) _view_function中的文件“ C:\ Users \ Gamer \ Documents \ AWS-SakMed \ backend \ SakMed \ app.py”,第51行 返回包装(* args,** kwargs) 文件“”,第2行,在get_cases中 文件“ c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py”,第528行,位于new_func中 结果= func(* args,** kwargs) 文件“ C:\ Users \ Gamer \ Documents \ AWS-SakMed \ backend \ SakMed \ app.py”,第89行,在get_cases中 查询= db.select(db.Provider中p的p.first_name) 在选择的文件“ c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py”中,行881 如果不是select_re.match(sql):sql ='select'+ sql TypeError:预期的字符串或类似字节的对象
逐步进行pdb调试(格式有点奇怪):
c:\ users \ gamer \ documents \ aws-sakmed \ backend \ sakmed \ app.py(89)get_cases()
->查询= db.select(db.Provider中p的p.first_name)(Pdb)步骤
-致电-c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py(3927) iter ()-> def iter (实体):(Pdb)c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py(3928) iter ()
->返回EntityIter(entity)(Pdb)
--call-- c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py(3630) init ()-> def init ((自身,实体):(Pdb)c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py( 3631)初始化()
-> self.entity =实体(Pdb)
--return-- c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py(3631) init ()-> None-> self.entity =实体(Pdb)
-返回-c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py(3928) iter ()->
->返回EntityIter(entity)(Pdb)
-致电-c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py(879)select()-> @cut_traceback(Pdb)c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py(881)select()
->如果不是select_re.match(sql):sql ='select'+ sql(Pdb)pp(sql)生成器对象get_cases.locals.genexpr at 0x048062B0(Pdb)
步骤TypeError:预期的字符串或类似字节的对象 c:\ users \ gamer.virtualenvs \ backend-qptpobgm \ lib \ site-packages \ pony \ orm \ core.py(881)select() ->如果不是select_re.match(sql):sql ='select'+ sql(Pdb)
相关代码:app.py
db = create_database()
datastore = DataStore(db)
def app_db_session(func):
wrapped = db_session(func)
def _view_function(*args, **kwargs):
global db_is_bound
if not db_is_bound:
debug = os.getenv('localdev')
if debug is None:
datastore.connect(host, name, password, dbname)
elif debug == 'True':
datastore.connect('localhost', 'user', 'password', 'local-db')
db_is_bound = True
return wrapped(*args, **kwargs)
return _view_function
@app.route('/recipient/{rec_id}/cases', methods=['GET'])
@app_db_session
def get_cases(rec_id):
query = db.Provider.select(lambda p: p.id == 1)
query = db.select(p.first_name for p in db.Provider))
相关代码:data_store.py
class DataStore():
def __init__(self, db):
self.db = db
def connect(self, host, user, passwd, db_name):
self.db.bind(provider='mysql', host=host, user=user, passwd=passwd, db=db_name)
self.__map_data_models()
def bind_memory(self):
self.db.bind(provider='sqlite', filename=':memory:')
self.__map_data_models()
def __map_data_models(self):
self.db.generate_mapping(create_tables=True)
相关代码:base.py
def create_database():
db = Database()
class Provider(db.Entity):
id = PrimaryKey(int, auto=True)
hsa_id = Required(str)
role = Optional(str)
available = Required(bool)
first_name = Optional(str)
last_name = Optional(str)
return db
答案 0 :(得分:1)
如果要使用生成器语法,则需要使用select
函数:
from pony import orm
...
query = orm.select(p for p in Person if p.name.startswith('A'))
for obj in query:
print(obj.name)
select
对象的方法Database
用于原始SQL查询
from pony import orm
...
db = orm.Database('sqlite', ':memory:')
...
rows = db.select("id, name FROM person p WHERE p.name LIKE 'A%'")
for row in rows:
print(row[1])