2015-07-06 22 views
6

Scala derleme hatasının herhangi bir bilgi bulunamadı. Ben kaygan 3.0 kullanıyorum ve sorun benim kimlik alanını temsil etmek Birini kullanıyorum arada ilgilidir inanıyoruzvalue ~ bir slick.lifted.Rep üyesi değil [Option [Int]]

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

bir derleme hatası alıyorum.

this answer'da önerildiği gibi kimlik alanına id.? eklemeyi denedim, ancak yine de aynı şekilde bir derleme hatası alıyorum. Kaygan 3.0'da bir şey değişti mi? aşağıdaki gibi

Benim kodudur:

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 
    } 
} 

cevap

3

Sorunun bazı semboller başka sorunlar için bir göz here atın kaygan 3.0.0

yılında eskisi gibi kullanılmıyor olmasıdır düşünüyorum

Eğer sorunlu çizgi, ne yapacağınıza bağlı olarak böyle bir şey olacaktır, ancak bu işe yarayacaktır:

def * = (Id, isim, talimatlar, katkı maddeleri) <> (. Tupled (Recipe.apply _) Recipe.unapply _)

Ayrıca implicits

ithalat ve bir sorun ile orada da gerek yok seçeneğiniz [Int] parametresi: belki bu daha iyi olmalı:

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 
    } 
} 

Veya ekleyerek. yorumlar kısmında konuşmak gibi saha id, Seçenek kullanarak whithout, bu yaklaşımın iyi olduğunu düşünüyorum

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

deneyin Yeni kod, Ben test ve bu derleme, Opsiyon ile bir sorun var, ben gerçekten bu – anquegi

+0

ihtiyacım var emin değilim Bu satırı değiştirme ve Seçenek [Int] ID sütununa ekleme VEYA ekleyerek. kimlik alanına ve Seçenek işlerini eklemiyor. – GoldenFish

+0

Teşekkürler, üçüncü seçeneğe de cevabı ekledim. – anquegi

2

@anquegi işaret şekilde field1 ~ field2 yapı aslında basitçe, kaputun altında bir (field1, field2) tuple inşa ediyor Bir tuple kullanmak için * projeksiyon değiştirerek doğrudan çalışır. Eğer tuple inşa etmek ~ kullanmak istiyorsanız

Alternatif olarak, TupleMethods ithal ederek geri alabilirsiniz (Slick 2.0 ~ olarak was moved out of the normal import scope.):

import slick.util.TupleMethods._ 

Ayrıca bkz: Slick 2.0 - update two or more columns

+0

Teşekkür ederim, bu ithalatı arıyordum :) –