3

Using Python SQLModel, I want to truncate a table, or remove all rows, and get the number of deleted rows, in the most SQLModel standar way. How can I do this? I am using this:

        with Session(engine) as session:
            count = session.exec(delete(MyModel)).fetchall()
            session.commit()

But it raises an error:

ResourceClosedError('This result object does not return rows. It has been closed automatically.')

I have also tried scalar() and fetchone() instead of fetchall() without success.

okelet
  • 716
  • 1
  • 8
  • 23

1 Answers1

4
with Session(engine) as session:
    statement = delete(MyModel)
    result = session.exec(statement)
    session.commit()
    print(result.rowcount)

Matched Row Counts

r-m-n
  • 14,192
  • 4
  • 69
  • 68