SQLAlchemy:“表"对象没有属性“id" [英] SQLAlchemy: 'Table' object has no attribute 'id'

查看:20
本文介绍了SQLAlchemy:“表"对象没有属性“id"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 和 SQLAlchemy 的新手,遇到了一个奇怪的问题,如下所示.

I'm a new learner on Python and SQLAlchemy, and I met a curious problem as below.

user = Table('users', meta, autoload=True, autoload_with=engine)

然后我

print(user.columns)

它工作正常,输出是 user.ID、user.Name 等.但是:

it works fine, the output are user.ID, user.Name, etc. But then:

Session = sessionmaker(bind=engine)
session = Session()

session.query(user).order_by(user.id)

显示错误:

AttributeError: 'Table' object has no attribute 'id'

我将id"更改为Name",这是同样的错误.我也试过 filter_by 方法,同样的错误.

I change the "id" to "Name", it's the same error. I also tried the filter_by method, the same error.

为什么会这样?

推荐答案

您可以使用:

session.query(user).order_by(user.c.id)

这篇关于SQLAlchemy:“表"对象没有属性“id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆