scala Intellij IDEA 15.0.2中的slick 2.0错误row._1错误

时间:2016-01-02 08:06:40

标签: scala intellij-idea slick-2.0

以下是我在Scala IntelliJ IDEA 15.0.2中的slick 2.0程序。

此程序在IntelliJ IDEA中发出编译时错误:

  

无法解析符号_1

我也附上了错误的屏幕截图。

import java.sql.Timestamp
import scala.slick.driver.PostgresDriver.simple._

case class User(
                 id: Long,
                 username: String,
                 email: Option[String],
                 password: String,
                 created: Timestamp)

//a simple table called 'users'
class Users(tag: Tag) extends Table[User](tag, "users") {

  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def username = column[String]("username", O.NotNull)
  // an Option[] in the case class maps to a Nullable field here
  def email = column[String]("email", O.Nullable)
  def password = column[String]("password", O.NotNull)
  // this is a hack for postgresql; if you're using another DB, comment this out
  // and the corresponding field in the case class
  def created = column[Timestamp]("created_at", O.NotNull, O.DBType("timestamp default now()"))

  // usernames should be unique
  def idx = index("users_unique_username", (username), unique = true)

  //define the "shape" of a single data record
  //we're saying that an object of class User (our case class) should be returned
  def * = (id, username, email.?, password,created) <> (User.tupled, User.unapply)
}

val connectionUrl = "jdbc:postgresql://localhost/slick?user=slick&password=slick"
Database.forURL(connectionUrl, driver = "org.postgresql.Driver") withSession {
  implicit session =>
    val users = TableQuery[Users]

    // SELECT * FROM users
    users.list foreach { row =>
      println("user with id " + row._1 + " has username " + row._2)
    }

    // SELECT * FROM users WHERE username='john'
    users.filter(_.username === "john").list foreach { row =>
      println("user whose username is 'john' has id "+row._1 )
    }
}

Slick error in intellij IDEA

当我在eclipse scala-ide中创建相同的程序时,程序运行正常并获取结果。

这是一个存在的问题还是我遗漏了一些东西?

1 个答案:

答案 0 :(得分:0)

看看这个

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

如果yor表返回Tuples,它看起来像

Table[(Long, String, Option[String] <...>]

然后你的

println("user with id " + row._1 + " has username " + row._2)

是有效的。我认为您的IDE在某种程度上配置错误并运行旧代码。这是您的代码可以修复以编译和工作的方式:

// SELECT * FROM users
users.list foreach { user =>
  println("user with id " + user.id + " has username " + user.name)
}


// SELECT * FROM users WHERE username='john'
users.filter(_.username === "john").list foreach { user =>
  println("user whose username is 'john' has id "+ user.id )
}