Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all entities from a slick query

Tags:

scala

slick

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)
  }
like image 751
Steven Dobay Avatar asked Nov 09 '22 12:11

Steven Dobay


1 Answers

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)
like image 57
Magnus Eklund Avatar answered Nov 15 '22 12:11

Magnus Eklund