我有两张桌子:
select * from "DEPARTMENTS";
ID | NAME | MANAGER_ID
----+-------+------------
1 | FOO | 1
3 | XXX | 2
4 | dept1 |
(3 rows)
select * from "EMPLOYEES";
NAME | LAST | EMAIL | PHONE | SKYPE | DEPT_ID | ID
------+------+------------------+-------+-------+---------+----
AAA | xxxx | abcdef@gmail.com | | | | 1
BBB | yyyy | | | | | 2
(2 rows)
我希望所有拥有经理姓名的部门(如果经理存在)共有3行。
然而,在我的架构中,当我这样做时:
import scala.slick.driver.PostgresDriver.simple._
import play.api.Play.current
case class DepartmentManager(id:Int,name:String,manager:String="")
case class Department(id:Option[Int],name:String,managerId:Option[Int])
class Departments (tag: Tag) extends Table[Department](tag, "DEPARTMENTS") {
val employees = TableQuery[Employees]
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
def name = column[String]("NAME", O.NotNull)
def managerId = column[Int]("MANAGER_ID", O.Nullable)
def manager = foreignKey("EMP_FK",managerId,employees)(_.id)
def * = (id.?,name,managerId.?) <> (Department.tupled, Department.unapply)
}
object Departments{
val db = play.api.db.slick.DB
val departments = TableQuery[Departments]
//this is the problematic query
def allWithMngr = db.withSession { implicit session =>
val employees = TableQuery[Employees]
val dm = (departments leftJoin employees ).list
dm.map(x =>DepartmentManager(x._1.id.getOrElse(0),x._1.name, x._2.name + " " + x._2.last))
}
}
我正在获得笛卡儿积(dm.size = 6)
试图像这样查询:
val dm = (departments leftJoin employees on((a,b)=> a.id === b.id)).list
导致错误:
[SlickException:读取ResultSet列Path的NULL值(null) s2._4]
这是我的员工模特:
case class Employee(name: String,last: String,email:Option[String]=None,phone:Option[String]=None,skype:Option[String]=None,department: Option[Int] = None, id: Option[Int] = None)
class Employees (tag: Tag) extends Table[Employee](tag, "EMPLOYEES") {
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
def name = column[String]("NAME", O.NotNull)
def last = column[String]("LAST", O.NotNull)
def email = column[String]("EMAIL", O.Nullable)
def phone = column[String]("PHONE", O.Nullable)
def skype = column[String]("SKYPE", O.Nullable)
def deptId = column[Int]("DEPT_ID", O.Nullable)
def dept = foreignKey("EMP_FK",deptId,departments)(_.id)
def * = (name,last,email.?,phone.?,skype.?,deptId.?, id.?) <> (Employee.tupled, Employee.unapply)
val departments = TableQuery[Departments]
}
作为输出我希望等效于这个本机Sql:
val query =sql""" select x."ID", x."NAME", y."NAME" from "DEPARTMENTS" x LEFT OUTER JOIN "EMPLOYEES" y ON (x."MANAGER_ID" = y."ID");""".as[DepartmentManager].list
将导致:
List (DepartmentManager(1,"FOO","AAA"),DepartmentManager(3,"XXX","BBB"),DepartmentManager(4,"FOO",""))
答案 0 :(得分:1)
你在这里遇到的是Slick 2处理连接中的空值的尴尬方式。 Slick 3中的情况更好,但我们可以在Slick 2中使用联接。
您的查询...
(departments leftJoin employees ).list
...将是一个交叉加入,因为你没有join
。
使用join
时遇到的错误是因为您要选择的列之一为NULL。使用Slick 2,您必须明确选择这些列并使用.?
将其提升为选项:
val query =
departments.leftJoin(employees).on(_.managerId === _.id )
.map { case (d, e) => (d.id, d.name, e.name.?) }
请注意e.name.?
---因为该部门可能没有e
(员工)。
此查询与测试数据的结果是:
(1, FOO, Some(AAA))
(2, XXX, Some(BBB))
(3, dept1, None)
......我认为这就是你所追求的目标。