如下代码: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")给我这个错误:AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with FooBar.bar has an attribute 'foo'关于为什么会发生这种情况的任何解释,以及如何实现这种事情的指南?
3 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
我遇到了同样的错误Neither 'InstrumentedAttribute' object nor 'Comparator' has an attribute
,但就我而言,问题是我的模型包含一个名为的列query
,该列将覆盖内部属性model.query
。
我决定将该列重命名为query_text
,从而消除了该错误。另外,传递name=
参数列方法会工作:query = db.Column(db.TEXT, name='query_text')
。
添加回答
举报
0/150
提交
取消