gpt4 book ai didi

sqlalchemy - get和filter不能混用吗?

转载 作者:行者123 更新时间:2023-12-05 02:16:40 24 4
gpt4 key购买 nike

当我尝试获取下一个查询时:

answer = sess.query(User).filter(User.id==1).get(1)

我收到错误消息:sqlalchemy.exc.InvalidRequestError: Query.get() 正在使用现有条件的查询上调用。
查询:

answer = sess.query(User).get(1)

工作正常。

为什么第一个不起作用?

我的类(class)定义:

class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
adr = relationship('Address', backref='uuu')

最佳答案

来自 Query.get 的文档:

get() is only used to return a single mapped instance, not multiple instances or individual column constructs, and strictly on a single primary key value. The originating Query must be constructed in this way, i.e. against a single mapped entity, with no additional filtering criterion. Loading options via options() may be applied however, and will be used if the object is not yet locally present.

关于sqlalchemy - get和filter不能混用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49113958/

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