Slick 2.0:删除多行 [英] Slick 2.0: Delete multiple rows

查看:130
本文介绍了Slick 2.0:删除多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是使用此功能从表中删除N个最早的ID:

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

此操作将抛出SlickException,因为

对DELETE语句的查询必须解析为具有单个表的理解-不支持的形状:Comprehension(fetch = None,offset = None)

A query for a DELETE statement must resolve to a comprehension with a single table -- Unsupported shape: Comprehension(fetch = None, offset = None)

如文档中所述:

用于删除的查询只能从单个表中选择.任何投影都会被忽略(它总是删除整行).

A query for deleting must only select from a single table. Any projection is ignored (it always deletes full rows).

什么触发了多重表?是因为sortBy子句创建了一个临时表来存储结果吗?目前,我将查询重写为:

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)

基本上是哪个ID,对于每个过滤器并删除它们,是否有一种更具可读性和直接性的方式即可一次删除多行?

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?

推荐答案

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

Try MyTable.filter(_.id in MyTable.sortBy(_.id.asc).take(size).map(_.id)).delete. Without looking at the details right now, I am assuming the error message to be misleading and the cause to be ORDER BY and LIMIT to not be supported for DELETE.

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

这篇关于Slick 2.0:删除多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆