属性错误:“InstrumentedList”对象没有属性

发布于 2024-12-08 08:24:16 字数 1319 浏览 1 评论 0原文

我有这些表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆橘 2024-12-15 08:24:16

如文档中所述,此处: https://docs .sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-one,您必须将 uselist=False 添加到关系中,而不是添加到 backref 中。

thing = relationship('Thing', backref=backref('voteinfo', 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))
余生共白头 2024-12-15 08:24:16

添加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!!

鲜血染红嫁衣 2024-12-15 08:24:16

事情发生了多么疯狂的变化,但这帮助了很多!,当我使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文