用光滑做我的第一步,我有这个表
case class Employee(name: String,last: String,department: Option[Int] = None,id: Option[Int] = None)
class Employees (tag: Tag) extends Table[Employee](tag, "EMPLOYEES") {
// Auto Increment the id primary key column
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
def name = column[String]("NAME", O.NotNull)
def last = column[String]("LAST", O.NotNull)
def dept = foreignKey("EMP_FK",deptId,departments)(_.id)
def * = (name,last,deptId.?, id.?) <> (Employee.tupled, Employee.unapply)
val departments = TableQuery[Departments]
}
case class DepartmentManager(id:Int,name:String,manager:String)
case class Department(id:Option[Int],name:String,managerId: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)
}
但我收到编译错误:
Query[Nothing,Nothing,Seq] 类型的表达式不符合预期的类型 List[DepartmentManager
我试过做这样的事情(只是为了检查,我知道这很糟糕):
def all: List[DepartmentManager] = db.withSession { implicit session =>
val employees = TableQuery[Employees]
val x = for {
(d, e) <- departments join employees
} yield (d.id, d.name, e.name + " " + e.last)
x.iterator.map(t=> DepartmentManager(t._1,t._2,t._3)).toList
}
但它没有给我想要的结果 - 后者 (t._1,t._2,t._3) 的结果看起来像
(1,Foo, (EMPLOYEES Path @1076478352._2).NAME (EMPLOYEES Path @1076478352._2).LAST)
请指教