Python 查询时出现 AttributeError:“InstrumentedAttribute”对象和“Comparator”都没有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16589208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:05:56  来源:igfitidea点击:

AttributeError while querying: Neither 'InstrumentedAttribute' object nor 'Comparator' has an attribute

pythonsqlalchemyattributeerror

提问by Bleeding Fingers

The following code:

以下代码:

Base = declarative_base()
engine = create_engine(r"sqlite:///" + r"d:\foo.db",
                       listeners=[ForeignKeysListener()])
Session = sessionmaker(bind = engine)
ses = Session()

class Foo(Base):
    __tablename__ = "foo"
    id = Column(Integer, primary_key=True)
    name = Column(String, unique = True)

class Bar(Base):
    __tablename__ = "bar"
    id = Column(Integer, primary_key = True)
    foo_id = Column(Integer, ForeignKey("foo.id"))

    foo = relationship("Foo")


class FooBar(Base):
    __tablename__ = "foobar"
    id = Column(Integer, primary_key = True)
    bar_id = Column(Integer, ForeignKey("bar.id"))

    bar = relationship("Bar")



Base.metadata.create_all(engine)
ses.query(FooBar).filter(FooBar.bar.foo.name == "blah")

is giving me this error:

给我这个错误:

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with FooBar.bar has an attribute 'foo'

Any explanations, as to why this is happening, and guidance to how such a thing could be achieved?

关于为什么会发生这种情况的任何解释,以及如何实现这样的事情的指导?

采纳答案by ostrokach

This is because you are trying to access barfrom the FooBarclass rather than a FooBarinstance. The FooBarclass does not have any barobjects associated with it--baris just an sqlalchemy InstrumentedAttribute. This is why you get the error:

这是因为您试图barFooBar类而不是FooBar实例进行访问。该FooBar班没有任何bar与它-关联的对象bar仅仅是一个SQLAlchemy的InstrumentedAttribute。这就是您收到错误的原因:

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with FooBar.bar has an attribute 'foo'

You will get the same error by typing FooBar.bar.foo.nameoutside the sqlalchemy query.

通过FooBar.bar.foo.name在 sqlalchemy 查询之外键入,您将得到相同的错误。

The solution is to call the Fooclass directly:

解决办法是Foo直接调用类:

ses.query(FooBar).join(Bar).join(Foo).filter(Foo.name == "blah")

回答by Bleeding Fingers

I cannot explain technically what happens but you can work around this problem by using:

我无法从技术上解释会发生什么,但您可以使用以下方法解决此问题:

ses.query(FooBar).join(Foobar.bar).join(Bar.foo).filter(Foo.name == "blah")

回答by qris

A related error that can be caused by configuring your SQLAlchemy relationships incorrectly:

错误配置 SQLAlchemy 关系可能导致的相关错误:

AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'corresponding_column'

In my case, I incorrectly defined a relationship like this:

就我而言,我错误地定义了这样的关系:

namespace   = relationship(PgNamespace, id_namespace, backref="classes")

The id_namespaceargument to relationship()should just not be there at all. SQLAlchemy is trying to interpret it as an argument of a different type, and failing with an inscrutable error.

id_namespace论点relationship()根本不应该存在。SQLAlchemy 试图将其解释为不同类型的参数,但由于一个难以理解的错误而失败。

回答by tokenizer_fsj

I was getting the same error Neither 'InstrumentedAttribute' object nor 'Comparator' has an attribute, but in my case, the problem was my model contained a Column named query, which was overwriting the internal property model.query.

我遇到了同样的错误Neither 'InstrumentedAttribute' object nor 'Comparator' has an attribute,但就我而言,问题是我的模型包含一个名为 的列query,它覆盖了内部属性model.query

I decided to rename that Column to query_textand that removed the error. Alternatively, passing the name=argument to the Column method would have worked: query = db.Column(db.TEXT, name='query_text').

我决定将该列重命名为query_text并消除了错误。另外,传递name=参数列方法会工作:query = db.Column(db.TEXT, name='query_text')