我有这些桌子:
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添加到关系中。我试着把与事物的关系从votesing换成Voteinfo,但这也没用。我不知道什么是工具列表。怎么回事?
如文档中所述,这里:https://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-one,您必须将uselist=False添加到backref,而不是添加到关系中。
相关问题 更多 >
编程相关推荐