我需要构建一个复杂的查询,我更喜欢使用原始的postgres。但是查询返回一个sqlalchemy.engine.result.ResultProxy
对象,当我迭代时,每个对象都是sqlalchemy.engine.result.RowProxy
对象
search_term = "%{0}%".format(d)
items = db.engine.execute("SELECT inventory.id,inventory.location,inventory.amount,inventory.units, plant_material.variety FROM inventory LEFT JOIN plant_material ON inventory.plant_material_id = plant_material.id WHERE plant_material.variety ILIKE %s", (search_term))
此标准查询返回我在Marshmallow中定义的模型对象<class 'app.models.Inventory'>:
Inventory.query.filter(Inventory.common_name.like(search_term)).limit(10)
如何返回models.Inventory对象并使用raw sql?
如果您对我的模型定义感到好奇,请查看此gist
答案 0 :(得分:0)
您可以使用以下内容:
query = db.engine.execute("SELECT inventory.id...")
for row in query:
print(row['id'])
...