Search code examples
pythonsqlalchemy

sqlachemy: 'tuple' object has no attribute 'label'


I have a model

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.id shows

<sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f3608b0d620>

where as

Question.stopped

(<sqlalchemy.orm.properties.MappedColumn at 0x7f3609101750>,)

Because of the above i am getting error when trying to do

            questions_query = (
                select(
                    Question.id.label('id'),
                    Question.is_sent_by_user.label('is_sent_by_user'),
                    Question.related_to.label('related_to'),
                    Question.stopped.label('stopped'),
                    Question.text.label('text'),
                    Question.id.label('order_by')
                )
                .where(Question.is_sent_by_user == True)
                .where(Question.chat_id == 49)
            )
at                    Question.stopped.label('stopped'),

i get
AttributeError: 'tuple' object has no attribute 'label'

Solution

  • There is a comma at the end of 'stopped' field define row.

    change

    stopped: Mapped[bool] = mapped_column(default=False),
    

    to

    stopped: Mapped[bool] = mapped_column(default=False)