What I wanted to do is delete the N oldest ids from my table using this function:
def deleteOldRows(size: Int)(implicit s: Session): Int =
MyTable.sortBy(_.id.asc).take(size).delete
This operation throws a SlickException
because
A query for a DELETE statement must resolve to a comprehension with a single table -- Unsupported shape: Comprehension(fetch = None, offset = None)
As stated also on the documentation:
A query for deleting must only select from a single table. Any projection is ignored (it always deletes full rows).
What is triggering the multiple table? Is it because the sortBy
clause creates a temporary table to store the results? For the moment I rewrote my query to this:
MyTable.sortBy(_.id.asc).take(size).list().map(result => MyTable.filter(_.id === result.id).delete)
Which is basically take the ids and for each one filter and delete, is there a more readable and direct way to delete multiple rows at once?