sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

✨ Allow deleting all rows from a table

Open jnewbery opened this issue 8 months ago • 2 comments

The sqlmodel docs explain how to delete a single row from a table.

It's also useful to be able to delete multiple rows (or even all rows) at the same time. See https://stackoverflow.com/questions/76648956/how-to-delete-all-rows-matching-a-condition-using-sqlmodel for example.

session.exec(delete(Table)) works fine, but triggers a type warning:

Pyright: Error: No overloads for "exec" match the provided arguments [reportCallIssue]
Pyright: Error: Argument of type "Delete" cannot be assigned to parameter "statement" of type "SelectOfScalar[_TSelectParam@exec]" in function "exec"
  "Delete" is not assignable to "SelectOfScalar[_TSelectParam@exec]" [reportArgumentType]

Adding an additional overload to Session.exec() allows this syntax to be used.

jnewbery avatar Apr 25 '25 09:04 jnewbery

Thanks for looking at this @svlandeg . I've fixed the failing pydantic error and marked this as ready for review.

jnewbery avatar May 06 '25 10:05 jnewbery

This would be great to have – I just ran into this error!

I think https://github.com/fastapi/sqlmodel/pull/1342 solves the same problem using UpdateBase, which covers update operations as well as delete ones.

rhwlo avatar May 09 '25 18:05 rhwlo

@jnewbery, thanks for your interest and efforts!

There is another PR #1342 that solves this problem in a more general way (also solves it for insert, update and text statements). Let's close this PR and focus on #1342.

Thanks again!

YuriiMotov avatar Aug 22 '25 14:08 YuriiMotov