0

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?

4

1 回答 1

2

试试MyTable.filter(_.id in MyTable.sortBy(_.id.asc).take(size).map(_.id)).delete。现在不查看详细信息,我假设错误消息具有误导性,并且 DELETE 不支持 ORDER BY 和 LIMIT 的原因。

我创建了一张票:https ://github.com/slick/slick/issues/872

于 2014-06-29T12:27:01.760 回答