Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
7
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@cobonas

SlickでのSession周り

こんなのがあった場合、

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)
  }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
7
Help us understand the problem. What are the problem?