【问题标题】:Why does compilation fail with "not found: value Users"?为什么编译失败并显示“未找到:值用户”?
【发布时间】:2015-01-11 08:43:40
【问题描述】:

我想从我的默认数据库postgres 中检索一行。我已经定义了表“用户”。

conf/application.conf

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5234/postgres"
db.default.user="postgres"
db.default.password=""

controllers/Application.scala

package controllers

import models.{UsersDatabase, Users}
import play.api.mvc._

object Application extends Controller {

  def index = Action { 
    Ok(views.html.index(UsersDatabase.getAll))
  }
}

models/Users.scala

package models

import java.sql.Date
import play.api.Play.current
import play.api.db.DB
import slick.driver.PostgresDriver.simple._

case class User(
    id: Int,
    username: String,
    password: String,
    full_name: String,
    email: String,
    gender: String,
    dob: Date,
    joined_date: Date
)

class Users(tag: Tag) extends Table[User](tag, "Users") {

    def id = column[Int]("id")
    def username = column[String]("username", O.PrimaryKey)
    def password = column[String]("password")
    def full_name = column[String]("full_name")
    def email = column[String]("email")
    def gender = column[String]("gender")
    def dob = column[Date]("dob")
    def joined_date = column[Date]("joined_date")
    def * = (id, username, password, full_name, email, gender, dob, joined_date) <> (User.tupled, User.unapply)
}

object UsersDatabase {

    def getAll: List[User] = {
        Database.forDataSource(DB.getDataSource()) withSession {
            Query(Users).list
        }
    }
}

访问http://localhost:9000/时出现编译错误:

[error] .../app/models/Users.scala:36: not found: value Users
[error]             Query(Users).list
[error]                   ^
[error] one error found
[error] (compile:compile) Compilation failed

如何解决此错误并正确访问数据?

【问题讨论】:

    标签: postgresql scala playframework-2.0 slick


    【解决方案1】:

    编译错误消息说明了一切 - 没有值 Users 在范围内使用。

    将对象 UsersDatabase 更改为如下所示:

    object UsersDatabase {
    
        val users = TableQuery[Users]
    
        def getAll: List[User] = {
            Database.forDataSource(DB.getDataSource()) withSession { implicit session =>
                users.list
            }
        }
    }
    

    由于您使用本地 val users 列出数据库中的用户,因此错误消失了。

    如 Slick 官方文档中的 Querying 所述,session val 是 list 的隐式值(如 final def list(implicit session: SessionDef): List[R]),因此块中的 implicit session

    执行查询的所有方法都采用隐式 Session 值。的 当然,如果您愿意,也可以显式传递会话:

    val l = q.list(session)
    

    【讨论】:

    • 感谢您的大力支持,非常感谢。但是运行这个给了我更多的错误[error] found : play.api.mvc.Result [error] required: slick.driver.PostgresDriver.backend.Session =&gt; play.api.mvc.Result [error] (which expands to) slick.driver.PostgresDriver.backend.SessionDef =&gt; play.api.mvc.Result [error] Ok(views.html.index(UsersDatabase.getAll)) [error] ^ [error] one error found [error] (compile:compile) Compilation failed [error] application - index.scala.html @(users: List[User]) &lt;h1&gt;Compiled&lt;/h1&gt;
    • 那是什么?我想将其添加到答案中。
    • 这是我的愚蠢错误,再次用控制器中的数据库会话包装结果:def index = Action { Database.forDataSource(DB.getDataSource()) withSession { ... Ok(views.html.index (UsersDatabase.getAll)) } } 当我问这个问题时,它没有完成。您的编辑和回答绝对正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 2014-12-31
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多