如何使用SqlAlchemy通过id查询数据库?

时间:2022-04-12 06:36:22

I need to query a SQLAlchemy database by its id something similar to

我需要通过类似的id查询SQLAlchemy数据库

User.query.filter_by(username='peter')

User.query.filter_by(用户名=“彼得”)

but for id. How do I do this? [Searching over Google and SO didn't help]

但对于id,我该怎么做呢?[搜索谷歌,所以没有帮助]

2 个解决方案

#1


63  

Query has a get function that supports querying by ID.

查询有一个get函数,支持通过ID查询。

For example, to query for an object with ID of 23: User.query.get(23)

例如,要查询ID为23的对象:User.query.get(23)

#2


23  

You can query an User with id = 1 like this

可以像这样用id = 1查询用户

session.query(User).get(1)

session.query(用户). get(1)

#1


63  

Query has a get function that supports querying by ID.

查询有一个get函数,支持通过ID查询。

For example, to query for an object with ID of 23: User.query.get(23)

例如,要查询ID为23的对象:User.query.get(23)

#2


23  

You can query an User with id = 1 like this

可以像这样用id = 1查询用户

session.query(User).get(1)

session.query(用户). get(1)