Steven Dobay
Steven Dobay

Reputation: 331

Getting all entities from a slick query

How to read all entities from a Query[TableType] or from a Query[TableType, EntityType, Seq] in slick 3.0.0? In the tutorial there was "result" method, but it isn't defined after all the configurations.

Edit:

I've tried to use qbooks.result and (for(book <- qbooks) yield(book)).result from this model:

import java.sql.Date
import slick.driver.H2Driver.api._
import slick.backend.DatabasePublisher
import slick.driver.JdbcProfile
import entities._

object tables {
  private val db = Database.forConfig("h2db")

  //one of the table queries
  val qbooks = TableQuery[Books]

  db.run(
    DBIO.seq(
      qbooks.schema.create,
      ...
    )
  )

  //one of the tables
  class Books(tag: Tag) extends Table[Book](tag, "books") {
    def isbn = column[Int]("isbn", O.PrimaryKey, O.AutoInc)
    def author = column[String]("author")
    def title = column[String]("title")
    def year = column[Int]("edition_year")
    def amount = column[Int]("amount")
    def * = (isbn, author, title, year, amount) <>
        (Book.tupled, Book.unapply)
  }

Upvotes: 4

Views: 3973

Answers (2)

Andrea Ciccotta
Andrea Ciccotta

Reputation: 672

take a look at this:

https://github.com/cicco94/scala-akka-slick-demo/blob/master/src/main/scala/com/academy/service/UserService.scala

ie: db.run(TableQuery[UserTable].result)

Upvotes: 0

Magnus Eklund
Magnus Eklund

Reputation: 649

val qbooks = TableQuery[Books] appears to be a macro (do macros have to be enabled in the compiler?). I haven't used that syntax but the following compiles for me

//one of the table queries
  object qbooks extends TableQuery[Books](tag ⇒ new Books(tag)) {
    def all = qbooks.result
  }

db.run(qbooks.all)

Upvotes: 1

Related Questions