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


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")

gives me this error:

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

Any explanation as to why this is happening, and a guide on how to achieve this kind of thing?

ostrokach :

This is because you are trying to access barfrom the FooBarclass instead of the FooBarinstance . The FooBarclass doesn't have any barobject associated with it - barjust a SQLAlchemy InstrumentedAttribute . This is why you get the error:

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

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

The solution is to call the class Foodirectly :

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

Related


AttributeError: '' object has no attribute''

gentlemen. Dutch I have a question, I am using python 3 to code. The code is about getting the news of the website onto my canvas. But I keep getting this error which says: AttributeError: 'NewsFeed' object has no attribute 'canvas'. Here is my code: from tkin

AttributeError: object has no attribute

AKHIL MATHEW In my custom Odoo module I have to call a python function when a button is clicked. I followed the exact method from the documentation. But I get error as below.AttributeError: 'book.meeting' object has no attribute 'checkout_function' My code is