oke*_*let 3 python sqlalchemy sqlmodel
使用 Python SQLModel,我想以最 SQLModel 标准的方式截断表,或删除所有行,并获取已删除行的数量。我怎样才能做到这一点?我正在使用这个:
with Session(engine) as session:
count = session.exec(delete(MyModel)).fetchall()
session.commit()
Run Code Online (Sandbox Code Playgroud)
但它会引发一个错误:
ResourceClosedError('This result object does not return rows. It has been closed automatically.')
Run Code Online (Sandbox Code Playgroud)
我也尝试过scalar(),而fetchone()不是fetchall()没有成功。
with Session(engine) as session:
statement = delete(MyModel)
result = session.exec(statement)
session.commit()
print(result.rowcount)
Run Code Online (Sandbox Code Playgroud)