【发布时间】:2013-01-20 15:06:02
【问题描述】:
我使用Slick 1.0.0-RC1。我对表对象有这个定义:
object ProductTable extends Table[(Int, String, String, String, Double, java.sql.Date, Int, Option[Int], Int, Boolean)]("products") {
def id = column[Int]("productId", O.PrimaryKey, O.AutoInc)
def title = column[String]("title")
def description = column[String]("description")
def shortDescription = column[String]("shortDescription")
def price = column[Double]("price")
def addedDate = column[java.sql.Date]("addedDate")
def brandId = column[Int]("brandId")
def defaultImageId = column[Option[Int]]("defaultImageId")
def visitCounter = column[Int]("visitCounter")
def archived = column[Boolean]("archived")
def * = id ~ title ~ description ~ shortDescription ~ price ~ addedDate ~ brandId ~ defaultImageId ~ visitCounter ~ archived
}
我需要一个从数据库中选择 8 行的简单查询:
ProductTable.filter(_.title === "something")
.sortBy(_.visitCounter)
.map(_.title)
.take(8)
.selectStatement
输出是:
select x2.x3 from
(select x4.`title` as x3 from `products` x4
where x4.`title` = 'something'
order by x4.`visitCounter` limit 8) x2
如果我摆脱take() 方法:
ProductTable.filter(_.title === "something")
.sortBy(_.visitCounter)
.map(_.title)
.selectStatement
那么输出是:
select x2.`title` from `products` x2
where x2.`title` = 'something'
order by x2.`visitCounter`
所以我的问题是:为什么 Slick 的查询对象用take() 方法构造时会生成子查询?
附:如果可以相关,我将所有这些都使用 MySql 驱动程序
【问题讨论】:
-
翻转地图并拍摄
-
你试过了吗?它不会改变输出 SQL
-
发布到 Slick 用户组,Zeiger 比任何人都更清楚为什么会生成子选择......