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
相关产品推荐
相关产品推荐

