繁体   English   中英

如何在特征内找到scala对象

[英]how to find the scala object inside the trait

我在播放时使用slick 1.0.1版本,这是我的代码:User.scala:

case class User(id: Option[Int], name : String)
trait UserComponent {
this: Profile =>

import profile.simple._

object Users extends Table[User]("users") {
def id = column[Int]("id", O.PrimaryKey)
def name =  column[String]("name", O.NotNull)
def * = id.? ~ name <> (User, User.unapply _)

def add(user: User)(implicit session: Session) = {
  this.insert(user)
}

def countByName(name: String)(implicit session: Session) = {
  (for {
    user <- Users
    if (user.name === name)
  } yield(user)).list.size
}

}
}

Rescource.scala:

case class Resource(id: Option[Int] = None, owner: String, types: String)

// The static object that does the actual work - note the names of tables and fields in H2 are case sensitive and must be all caps 
object Resources extends Table[Resource]("RESOURCE") {
def id = column[Long]("ID", O.PrimaryKey, O.AutoInc)
def owner = column[Int]("Owner")
def types = column[String]("Type")
def withuser = foreignKey("User_FK", owner, models.User)(_.id)

}

错误是:

[error] F:\mysource\play-slick\app\models\Resource.scala:14: not found: value Us
erComponent
[error]   def withuser = foreignKey("User_FK", owner, UserComponent.Users)(_.id)
[error]                                               ^                ^

尝试

trait ResourceComponent {
 this: Profile with UserComponent => // depend on the user component
  object Resources extends Table[Resource]("RESOURCE") {
    ...
    def withuser = foreignKey("User_FK", owner, Users)(_.id)
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM