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

I have a Scala compilation error that I could not find any information on. I am using slick 3.0 and getting a compilation error

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

I believe the problem is with the way I use the option to represent my ID field.

I tried to add id.? in the id field as suggested in this answer , but I still get a compilation error in the same way. Has anything changed in slick 3.0?

My code is as follows:

 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 } } 
+6
source share
2 answers

I think the problem is that some characters are not used, as before, in slick 3.0.0

Look here for further issues.

in your case, the problematic line will be something like this depending on what you are going to do, but this should work:

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

Also you do not need import implicits

and there is a problem with the [Int] parameter: maybe this should be better:

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

Or add.? to the field id, without using the option, as we say in the comments, I think this aproach is better

 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 } } 
+3
source

The field1 ~ field2 construct actually builds a tuple (field1, field2) under the hood, so @anquegi indicates that simply changing the projection * to use the tuple will work directly.

Alternatively, if you want to use ~ to build a tuple, you can return it by importing TupleMethods (as ~ was moved from the regular import area in Slick 2.0.):

 import slick.util.TupleMethods._ 

See also: Slick 2.0 - Update two or more columns

+2
source

All Articles