user504909
user504909

Reputation: 9559

slick2.0: how to make a Table object?

Hi I just update and run the the simple slick Table and want to inquire it.

import scala.slick.driver.PostgresDriver.simple._
import scala.slick.lifted.TableQuery

class Coffees(tag: Tag) extends Table[(String, Double)](tag, "COFFEES") {
  def name = column[String]("COF_NAME", O.PrimaryKey)
  def price = column[Double]("PRICE")
  def * = (name, price)
}
val coffees = TableQuery[Coffees];

The error is:

[error] C:\testprojects\slickplay\app\model\Coffee.scala:11: expected class or o bject definition
[error] val coffees = TableQuery[Coffees];

The TableQuery[Coffees] do not return objects???how to fix it.

Upvotes: 3

Views: 532

Answers (3)

Roy Lin
Roy Lin

Reputation: 760

object coffees extends TableQuery(new Coffees(_))

Upvotes: 0

Sky
Sky

Reputation: 2607

Or you can put all things in side trait:

import scala.slick.driver.PostgresDriver.simple._

trait DomainComponent{

   class Coffees(tag: Tag) extends Table[(String, Double)](tag, "COFFEES") {
     def name = column[String]("COF_NAME", O.PrimaryKey)
     def price = column[Double]("PRICE")
     def * = (name, price)
    }
  val coffees = TableQuery[Coffees];
   } 

Upvotes: 0

mattanja
mattanja

Reputation: 1452

You can't have a val outside of a class or object definition.

Try

object DatabaseContext {
  val coffees = TableQuery[Coffees]
}

Upvotes: 1

Related Questions