[Python] Why am I getting "Class 'sqlalchemy.engine.row.Row' is not mapped" when trying to...

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    I've just moved from SQLAlchemy 1.3 to 1.4 and am trying out the new 2.0 syntax. Given the following table:

    class Player(Base):

    __table_args__ = {"schema": "test"}
    __tablename__ = "player"

    id_ = sa.Column(sa.Integer, primary_key=True)
    name_ = sa.Column(sa.String(100))


    I add some records:

    dal.session.add_all([Player(1, "t1"), Player(2, "t2"), Player(3, "t3")])


    I can delete records using 1.x:

    del_records = session.query(Player).filter(Player.id_ <= 10)
    for d in del_records:
    session.delete(d)


    However, when I switch to 2.0:

    del_records = session.execute(sa.select(Player).where(Player.id_ <= 10))
    for d in del_records:
    session.delete(d)


    I get the following error:

    Exception has occurred: UnmappedInstanceError (note: full exception trace is shown but execution is paused at: <module>)
    Class 'sqlalchemy.engine.row.Row' is not mapped

    The above exception was the direct cause of the following exception:

    File "/Users/philipjoss/Documents/GitHub/polgara/sandpit.py", line 61, in <module> (Current frame)
    dal.session.delete(d)


    Any idea what's going on?

    Continue reading...

Compartilhe esta Página