You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQLAlchemy报错:'tuple' object has no attribute 'label' 问题求助

问题根源

你的Question.stopped字段定义末尾多了一个逗号,导致Python将赋值结果解析成了元组,而非SQLAlchemy的InstrumentedAttribute对象。这就是调用.label()时报错tuple has no attribute 'label'的核心原因。

解决方案

直接移除stopped字段行末尾的逗号即可:

修正后的模型代码:

class Question(Base):
    __tablename__ = 'messages'
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, index=True)
    guid: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True),default=uuid.uuid4,index=True, unique=True)
    text: Mapped[str]
    is_sent_by_user: Mapped[bool] = mapped_column(Boolean, default=False)
    chat_id = mapped_column(ForeignKey("chats.id"))
    chat: Mapped["Chat"] = relationship(back_populates="messages")
    stopped: Mapped[bool] = mapped_column(default=False)  # 移除了末尾的逗号
    related_to: Mapped[int] = mapped_column(default=None,nullable=True)
验证修正效果

修正后,Question.stopped的类型会和Question.id一致,显示为<sqlalchemy.orm.attributes.InstrumentedAttribute at ...>,此时执行原查询语句即可正常运行。

内容的提问来源于stack exchange,提问作者Santhosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:52:34