{"id":921,"hash":"4965c60ef1f59f630528033c8367abaad00fea69df57fe877b074f46e956d0eb","pattern":"AttributeError while querying: Neither &#39;InstrumentedAttribute&#39; object nor &#39;Comparator&#39; has an attribute","full_message":"The following code:\n\nBase = declarative_base()\nengine = create_engine(r\"sqlite:///\" + r\"d:\\foo.db\",\n                       listeners=[ForeignKeysListener()])\nSession = sessionmaker(bind = engine)\nses = Session()\n\nclass Foo(Base):\n    __tablename__ = \"foo\"\n    id = Column(Integer, primary_key=True)\n    name = Column(String, unique = True)\n\nclass Bar(Base):\n    __tablename__ = \"bar\"\n    id = Column(Integer, primary_key = True)\n    foo_id = Column(Integer, ForeignKey(\"foo.id\"))\n\n    foo = relationship(\"Foo\")\n\nclass FooBar(Base):\n    __tablename__ = \"foobar\"\n    id = Column(Integer, primary_key = True)\n    bar_id = Column(Integer, ForeignKey(\"bar.id\"))\n\n    bar = relationship(\"Bar\")\n\nBase.metadata.create_all(engine)\nses.query(FooBar).filter(FooBar.bar.foo.name == \"blah\")\n\nis giving me this error:\n\nAttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with FooBar.bar has an attribute 'foo'\n\nAny explanations, as to why this is happening, and guidance to how such a thing could be achieved?","ecosystem":"pypi","package_name":"sqlalchemy","package_version":null,"solution":"This is because you are trying to access bar from the FooBar class rather than a FooBar instance. The FooBar class does not have any bar objects associated with it--bar is just an sqlalchemy InstrumentedAttribute. This is why you get the error:\n\nAttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with FooBar.bar has an attribute 'foo'\n\nYou will get the same error by typing FooBar.bar.foo.name outside the sqlalchemy query.\n\nThe solution is to call the Foo class directly:\n\nses.query(FooBar).join(Bar).join(Foo).filter(Foo.name == \"blah\")","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/16589208/attributeerror-while-querying-neither-instrumentedattribute-object-nor-compa","votes":92,"created_at":"2026-04-19T04:52:00.972767+00:00","updated_at":"2026-04-19T04:52:00.972767+00:00"}