2016-01-02 78 views
1

下面是我在Scala IntelliJ IDEA 15.0.2中的slick 2.0程序。slick 2.0錯誤Scala Intellij IDEA中的row._1錯誤15.0.2

這個節目是給在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階-IDE所創建的相同的程序,該程序運行正常,並獲取結果。

這是一個存在的問題,還是我錯過了我的一面?

+0

在我看來,行實際上是用戶的一個實例。因此'._1'應該是'.id','._2'應該是'.username'。我不知道爲什麼這會在Eclipse中起作用。 – colinjwebb

回答

0

在這個

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

看看如果侑表中返回元組它看起來像

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) 
} 
+0

我不認爲代碼有問題,因爲代碼在intellij IDEA和eclipse scala IDE中都是一樣的。另外,兩個IDE中的scala版本都是2.11.7。對於您對代碼的建議,我們可以使用任何自定義Clase類對象的表或OOB數據類型的Tuple。 Slick支持他們兩人。 –