Edited at

SlickでのSession周り

More than 5 years have passed since last update.

こんなのがあった場合、


Model.scala

import play.api.db.DB

import play.api.Play.current
import scala.slick.driver.MySQLDriver.simple._
import scala.slick.jdbc._
import scala.language.postfixOps

case class Model(id:Int, name:String)

object Models extends Table[Model]("models") {

def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name", O.NotNull, O DBType "varchar(100)")
def * = id ~ name <> (Model.apply _, Model.unapply _)

/** DBコネクション */
val database = Database.forDataSource(DB.getDataSource())

/** 登録 */
def create(e: Model) = database.withTransaction { implicit session: Session =>
Models.insert(e)
}
}


Database.threadLocalSessionをimportするとimplicit ....がいらないらしい


Model.scala

import play.api.db.DB

import play.api.Play.current
import scala.slick.driver.MySQLDriver.simple._
import scala.slick.jdbc._
import Database.threadLocalSession
import scala.language.postfixOps

case class Model(id:Int, name:String)

object Models extends Table[Model]("models") {

def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name", O.NotNull, O DBType "varchar(100)")
def * = id ~ name <> (Model.apply _, Model.unapply _)

/** DBコネクション */
val database = Database.forDataSource(DB.getDataSource())

/** 登録 */
def create(e: Model) = database.withTransaction {
Models.insert(e)
}
}