AttributeError: InstrumentedList object has no attribute

I have table 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 condition "if value is True":

AttributeError: 'InstrumentedList' object has no attribute 'upvotes'

I tried providing Voteinfo with my own unique identifier and adding the uselist = False link to it. I tried replacing the relationship with the thing from VoteThing to Voteinfo, but that didn't help either. I do not know what InstrumentedList is. What's happening?

+5
source share
1 answer

As explained in the documentation, here: http://www.sqlalchemy.org/docs/orm/relationships.html#one-to-one , you need to add uselist = False, not in the relationship, but in the backref.

thing = relationship('Thing', backref=backref('voteinfo', uselist=False))
+10

All Articles