5
我有這些表的表:AttributeError的:「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
當我嘗試運行此,我得到了「如果值爲True」的條款在此錯誤代碼:
AttributeError: 'InstrumentedList' object has no attribute 'upvotes'
我已經嘗試給Voteinfo自己的唯一ID並添加uselist = False關係。我嘗試用VoteThing替換Voteinfo中的關係,但這也沒有幫助。我不知道InstrumentedList是什麼。到底是怎麼回事?
因爲'thing'是'__init__'的一個參數,大概你在實例化VoteThing時傳遞它。那麼你傳遞了什麼? –
是的,我路過一件事: thing1 =事情(), USER1 =用戶(), voteinfo1 = Voteinfo(thing1), votething1 = VoteThing(USER1,thing1,真) –