簡體   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