当我尝试执行Query(query.length).first
一个查询时,它表示 2 个表的连接,这些表具有多个具有相同名称的列,我得到了格式错误的 sql。考虑这个例子:
// in Main.scala
import scala.slick.driver.MySQLDriver.simple._
object Main extends App {
object Houses extends Table[Long]("Houses") {
def id = column[Long]("id")
def * = id
}
object Rooms extends Table[(Long, Long)]("Rooms") {
def id = column[Long]("id")
def houseId = column[Long]("houseId")
def * = id ~ houseId
}
val query = for {
h <- Houses
r <- Rooms
if h.id === r.houseId
} yield (h, r)
println("QUERY: " + Query(query.length).selectStatement)
}
// in build.sbt
scalaVersion := "2.10.2"
libraryDependencies += "com.typesafe.slick" %% "slick" % "1.0.1"
此示例生成以下 SQL:
select x2.x3 from
(select count(1) as x3 from
(select x4.`id`, x5.`id`, x5.`houseId`
from `Houses` x4, `Rooms` x5 where x4.`id` = x5.`houseId`) x6) x2
这显然是错误的,并且被 MySQL 拒绝,因为id
列select x4.id, x5.id
部分重复。
我可以尝试执行以下操作:
query.list.size
但这会从查询中提取所有行并通过网络发送它们,这将极大地影响性能。
我究竟做错了什么?有什么办法可以解决吗?