属性错误:“InstrumentedList”对象没有属性
我有这些表:
class Thing(Base):
__tablename__ = 'thing'
id = Column(Integer, primary_key=True)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
class Voteinfo(Base):
__tablename__ = 'voteinfo'
thing_id = Column(Integer, ForeignKey('thing.id'), primary_key=True)
thing = relationship('Thing', backref='voteinfo')
upvotes = Column(Integer)
downvotes = Column(Integer)
def __init__(self, thing)
self.thing = thing
class VoteThing(Base):
__tablename__ = 'votething'
id = Column(Integer, primary_key=True)
voter_id = Column(Integer, ForeignKey('voter.id'))
voter = relationship('Voter', backref='votescast')
thing_id = Column(Integer, ForeignKey('thing.id'))
thing = relationship('Thing', backref='votesreceived')
value = Column(Boolean)
def __init__(self, voter, thing, value):
if value is True:
thing.voteinfo.upvotes += 1
else:
thing.voteinfo.downvotes += 1
当我尝试运行此表时,我在“if value is True”子句中收到此错误代码:
AttributeError: 'InstrumentedList' object has no attribute 'upvotes'
我尝试为 Voteinfo 提供其自己的唯一 ID 并将 uselist=False 添加到关系中。我尝试将与事物的关系从 VoteThing 替换为 Voteinfo,但这也没有帮助。我不知道 InstrumentedList 是什么。到底是怎么回事?
I have these tables tables:
class Thing(Base):
__tablename__ = 'thing'
id = Column(Integer, primary_key=True)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
class Voteinfo(Base):
__tablename__ = 'voteinfo'
thing_id = Column(Integer, ForeignKey('thing.id'), primary_key=True)
thing = relationship('Thing', backref='voteinfo')
upvotes = Column(Integer)
downvotes = Column(Integer)
def __init__(self, thing)
self.thing = thing
class VoteThing(Base):
__tablename__ = 'votething'
id = Column(Integer, primary_key=True)
voter_id = Column(Integer, ForeignKey('voter.id'))
voter = relationship('Voter', backref='votescast')
thing_id = Column(Integer, ForeignKey('thing.id'))
thing = relationship('Thing', backref='votesreceived')
value = Column(Boolean)
def __init__(self, voter, thing, value):
if value is True:
thing.voteinfo.upvotes += 1
else:
thing.voteinfo.downvotes += 1
When I try to run this, I get this error code in the "if value is True" clause:
AttributeError: 'InstrumentedList' object has no attribute 'upvotes'
I've tried giving Voteinfo its own unique ID and adding uselist=False to the relationship. I've tried replacing the relationship to thing from VoteThing to Voteinfo, but that didn't help either. I don't know what an InstrumentedList is. What is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如文档中所述,此处: https://docs .sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-one,您必须将 uselist=False 添加到关系中,而不是添加到 backref 中。
As explained in the documentation, here : https://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-one, you have to add uselist=False not to the relationship, but to the backref.
添加lazy='dynamic'参数以请求不自动执行查询并当然刷新可能很有用(
thing=relationship('Thing',backref='voteinfo',lazy='dynamic')< /code>),否则会在内部发出表达式,调用 all() 返回“thing”列表,因此出现 AttributeError: 'InstrumentedList' object has no attribute 错误!
It may been useful to add lazy='dynamic' argument to request that the query is not automatically executed and of course refresh (
thing = relationship('Thing', backref='voteinfo', lazy='dynamic')
), otherwise expression is issued internally call all() to return the list of 'thing' hence AttributeError: 'InstrumentedList' object has no attribute error!!事情发生了多么疯狂的变化,但这帮助了很多!,当我使用lazy = 'dynamic'时出现错误,但这就是我的代码的样子
tool = db.relationship("Tool", backref="activities", uselist=False)
它给了我 db.model 类型,这就是我正在寻找的。而不是检测类列表类型。
crazy how things have changed but this helped a ton!, an error raised when i used lazy = 'dynamic' but this is what my code looks like
tool = db.relationship("Tool", backref="activities", uselist=False)
it gives me db.model type and it's what i was looking for. Instead of a instrumented class list type.