AttributeError: 'InstrumentedList' 对象没有属性 [英] AttributeError: 'InstrumentedList' object has no attribute
问题描述
我有这些表格:
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"子句中收到此错误代码:
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'
我尝试为 Voteinfo 提供自己的唯一 ID,并将 uselist=False 添加到关系中.我尝试将与事物的关系从 VoteThing 替换为 Voteinfo,但这也无济于事.我不知道 InstrumentedList 是什么.这是怎么回事?
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?
推荐答案
如文档中所述,此处:https://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-one,你必须添加 uselist=False 不到关系,但到反向引用.
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.
thing = relationship('Thing', backref=backref('voteinfo', uselist=False))
这篇关于AttributeError: 'InstrumentedList' 对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!