在 sqlalchemy 中执行外连接后如何访问列
我对两个表进行了外连接。我得到了正确的结果。但我不知道如何访问结果中的列。下面是我的代码。
结果 = db.session.query(Purchase, Product.pr_id).outerjoin(Product, Purchase.id == Product.pr_id).filter(Purchase.user_id==current_user.id, Product.status= =pr_status).order_by(desc(Purchase.cost)).all()
'result[0].Purchase.cost' 给出了第一次购买的成本。但 'result[0].Product.status' 给出 AttributeError: 无法在“Product”列的行中找到列。 为什么会出现这种情况?如何访问“状态”列
I did outerjoin of two tables. And I got the correct result. But I don't know how to access the Columns in the result. Below is my code.
result = db.session.query(Purchase, Product.pr_id).outerjoin(Product, Purchase.id == Product.pr_id).filter(Purchase.user_id==current_user.id, Product.status==pr_status).order_by(desc(Purchase.cost)).all()
'result[0].Purchase.cost' gives me cost of the first purchase. But 'result[0].Product.status' giving AttributeError: Could not locate column in row for column 'Product'.
Why this happen ? How can I access the 'status' column
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
db.session.query()
的第二个参数从Product.pr_id
更改为Product
应该可以。这是在 选择- orm-entities-and-attributes] 在示例中
Changing the second argument of
db.session.query()
fromProduct.pr_id
toProduct
should work.This is in the docs at selecting-orm-entities-and-attributes] in the example with