2015-07-06 18 views
6

Tôi có lỗi biên dịch Scala mà tôi không thể tìm thấy bất kỳ thông tin nào. Tôi đang sử dụng trơn 3.0 và đang nhận được một lỗi biên dịch của~ không phải là thành viên của slick.lifted.Rep [Tùy chọn [Int]]

value ~ is not a member of slick.lifted.Rep[Option[Int]]

Tôi tin rằng vấn đề này liên quan đến cách tôi đang sử dụng một lựa chọn để đại diện cho trường ID của tôi.

Tôi đã thử thêm id.? vào trường id như được đề xuất trong this answer nhưng tôi vẫn nhận được lỗi biên dịch trong cùng một tĩnh mạch. Có bất cứ điều gì thay đổi trong slick 3.0?

Mã của tôi là như sau:

import slick.driver.H2Driver.api._ 
import scala.concurrent.ExecutionContext.Implicits.global 

case class Recipe(id: Option[Int] = None, name: String, instructions: String, ingredients: String) 

object AddFixtures { 

    class Recipes(tag: Tag) extends Table[Recipe](tag, "recipe") { 
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc) 
    def name = column[String]("name") 
    def instructions = column[String]("instructions") 
    def ingredients = column[String]("ingredients") 

    def * = id ~ name ~ instructions ~ ingredients <> (Recipe, Recipe.unapply _) 
    } 

    val recipes = TableQuery[Recipes] 

    val setup = DBIO.seq(
    recipes.schema.create, 
    recipes += Recipe(None, "Chicken with Avocado", "Mix it up", "Chicken, Avocado") 
) 

    def apply() = { 
    val db = Database.forConfig("h2mem1") 

    try db.run(setup) 
    finally db.close 
    } 
} 

Trả lời

3

Tôi nghĩ rằng vấn đề là một số biểu tượng không được sử dụng như trước đây trong trơn 3.0.0

Hãy xem here cho vấn đề thêm

trong trường hợp dòng có vấn đề sẽ là một cái gì đó như thế này tùy thuộc vào những gì bạn sẽ làm, nhưng điều này sẽ làm việc:

def * = (Id, tên, hướng dẫn, các thành phần) <> ((Recipe.apply _) tupled., Recipe.unapply _)

Ngoài ra bạn không cần implicits nhập

và đó cũng là một vấn đề với thông số tùy chọn [Int] của bạn: có thể điều này sẽ tốt hơn:

import slick.driver.H2Driver.api._ 


object SlickStackOverflow extends App { 

} 

case class Recipe(id: Option[Int] = None, name: String, instructions: String, ingredients: String) 

object AddFixtures { 

    class Recipes(tag: Tag) extends Table[Recipe](tag, "recipe") { 
    def id = column[Option[Int]]("id", O.PrimaryKey, O.AutoInc) 
    def name = column[String]("name") 
    def instructions = column[String]("instructions") 
    def ingredients = column[String]("ingredients") 

    def * = (id, name, instructions, ingredients) <> ((Recipe.apply _).tupled, Recipe.unapply _) 
    } 

    val recipes = TableQuery[Recipes] 

    val setup = DBIO.seq(
    recipes.schema.create, 
    recipes += Recipe(None, "Chicken with Avocado", "Mix it up", "Chicken, Avocado") 
) 

    def apply() = { 
    val db = Database.forConfig("h2mem1") 

    try db.run(setup) 
    finally db.close 
    } 
} 

Hoặc thêm.? để id lĩnh vực, whithout sử dụng Option, như chúng ta nói trong ý kiến, tôi nghĩ rằng aproach này là tốt hơn

package org.example 

import slick.driver.H2Driver.api._ 

object SlickStackOverflow extends App { 

} 

case class Recipe(id: Option[Int], name: String, instructions: String, ingredients: String) 

object AddFixtures { 

    class Recipes(tag: Tag) extends Table[Recipe](tag, "recipe") { 
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc) 
    def name = column[String]("name") 
    def instructions = column[String]("instructions") 
    def ingredients = column[String]("ingredients") 

    def * = (id.?, name, instructions, ingredients) <> ((Recipe.apply _).tupled, Recipe.unapply _) 
    } 

    val recipes = TableQuery[Recipes] 

    val setup = DBIO.seq(
    recipes.schema.create, 
    recipes += Recipe(None, "Chicken with Avocado", "Mix it up", "Chicken, Avocado") 
) 

    def apply() = { 
    val db = Database.forConfig("h2mem1") 

    try db.run(setup) 
    finally db.close 
    } 
} 
+0

Thử mã mới, tôi đã thử nghiệm và biên dịch này, cũng có vấn đề với Tùy chọn, tôi không chắc chắn nếu bạn thực sự cần điều này – anquegi

+0

Thay đổi dòng đó và thêm Tùy chọn [Int] vào cột id HOẶC thêm.? vào trường id và không thêm tùy chọn hoạt động. – GoldenFish

+0

Cảm ơn, tôi cũng đã thêm tùy chọn thứ ba vào câu trả lời. – anquegi

2

Các field1 ~ field2 xây dựng là thực sự xây dựng một tuple (field1, field2) dưới mui xe, như vậy là @anquegi chỉ ra, chỉ đơn giản việc thay đổi chế độ chiếu * để sử dụng trực tiếp bộ túp sẽ hoạt động.

Ngoài ra, nếu bạn muốn sử dụng ~ để xây dựng các tuple bạn có thể lấy lại bằng cách nhập TupleMethods (như ~was moved out of the normal import scope trong Slick 2.0.):

import slick.util.TupleMethods._ 

Xem thêm: Slick 2.0 - update two or more columns

+0

Cảm ơn bạn, tôi đã tìm kiếm nhập đó :) –

Các vấn đề liên quan