gpt4 book ai didi

python - 小马ORM-解决 'Expected string or bytes-like object'错误

转载 作者:行者123 更新时间:2023-12-02 13:45:18 25 4
gpt4 key购买 nike

我当前正在使用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进行调试,但不确定这说明了什么。

堆栈跟踪:

Traceback (most recent call last): File "c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\chalice\app.py", line 842, in _get_view_function_response response = view_function(**function_args) File "C:\Users\Gamer\Documents\AWS-SakMed\backend\SakMed\app.py", line 51, in _view_function return wrapped(*args, **kwargs) File "", line 2, in get_cases File "c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py", line 528, in new_func result = func(*args, **kwargs) File "C:\Users\Gamer\Documents\AWS-SakMed\backend\SakMed\app.py", line 89, in get_cases query = db.select(p.first_name for p in db.Provider) File "c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py", line 881, in select if not select_re.match(sql): sql = 'select ' + sql TypeError: expected string or bytes-like object



逐步进行pdb调试(格式有点奇怪):

c:\users\gamer\documents\aws-sakmed\backend\sakmed\app.py(89)get_cases()

-> query = db.select(p.first_name for p in db.Provider) (Pdb) step(s)
--Call-- c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py(3927)iter()

-> def iter(entity): (Pdb) c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py(3928)iter()

-> return EntityIter(entity) (Pdb)
--Call-- c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py(3630)init()

-> def init(self, entity): (Pdb) c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py(3631)init()

-> self.entity = entity (Pdb)
--Return-- c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py(3631)init()->None

-> self.entity = entity (Pdb)
--Return-- c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py(3928)iter()->
-> return EntityIter(entity) (Pdb)
--Call-- 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()

-> if not select_re.match(sql): sql = 'select ' + sql (Pdb) pp(sql) generator object get_cases.locals.genexpr at 0x048062B0 (Pdb)
step(s) TypeError: expected string or bytes-like object c:\users\gamer.virtualenvs\backend-qptpobgm\lib\site-packages\pony\orm\core.py(881)select() -> if not 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

最佳答案

如果要使用生成器语法,则需要使用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])

关于python - 小马ORM-解决 'Expected string or bytes-like object'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55019046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com