slick

Catch Unique Key Exception Scala Slick

*爱你&永不变心* 提交于 2019-12-12 04:23:32
问题 Below is the code I use in inserting into the db however when the name of a group which is unique is entered I get a unique key violation in the logs override def create(groups: GroupEntity): Future[GroupEntity] = db.run{groupsTableQuery returning groupsTableQuery += groups} 回答1: Recover using recoverWith . As all exceptions will be of type PSQLException, check if certain keywords exist in message of the exception to handle it. val future = db.run { groupsTableQuery returning groupsTableQuery

How to reference a column by name in slick plain SQL?

久未见 提交于 2019-12-12 03:49:44
问题 I would like to use named references instead of positional in GetResult, so that instead of this: implicit val getCoffeeResult = GetResult(r => Coffee(r.<<, r.<<, r.<<)) i could write something like this: implicit val getCoffeeResult = GetResult(r => Coffee(r.get("name"), r.get("supID"),r.get("price"))) I can has named result? 回答1: You can get it through the resultset: r.rs.getString("name") 来源: https://stackoverflow.com/questions/15452732/how-to-reference-a-column-by-name-in-slick-plain-sql

How to write class and tableclass mapping for slick2 instead of using case class?

廉价感情. 提交于 2019-12-12 02:26:45
问题 I use case class to transform the class object to data for slick2 before, but current I use another play plugin, the plugin object use the case class, my class is inherent from this case class. So, I can not use case class as the scala language forbidden use case class to case class inherent. before: case class User() class UserTable(tag: Tag) extends Table[User](tag, "User") { ... def * = (...)<>(User.tupled,User.unapply) } it works. But now I need to change above to below: case class

Cannot roll back session in auto-commit mode with Slick

会有一股神秘感。 提交于 2019-12-12 02:26:40
问题 import scala.slick.driver.PostgresDriver private val postgres = PostgresDriver.simple.Database.forURL(url, driver = driver) def myMethod(testCode: Session => Any) { val session = postgres.createSession() session.conn.setAutoCommit(false) try { testCode(session) } finally { session.rollback() session.close() } sbt has following dependencies "com.typesafe.slick" %% "slick" % "2.1.0", "com.github.tminglei" %% "slick-pg" % "0.6.3" Though I have set autocomit to false I get following. I must be

Scala + Slick how to map json to the table column with datatype as blob

微笑、不失礼 提交于 2019-12-12 02:07:59
问题 I need to map json values to the table using slick and store whole json as a blob in the same table as well. def createJob(jobs: JobEntity): Future[Option[Long]] = db.run(job returning job.map((_.id)) += jobs) My Json contains values like id(long),name(string), type(string) while the table I am trying to map has columns id(long),name(string), type(string),json_data(blob) I have JobEntity class as case class JobEntity(id: Option[Long] = None, name: String, type: String) { require(!jobname

Trying to get all records from 2 weeks ago

陌路散爱 提交于 2019-12-12 01:45:54
问题 I'm trying to get all records from a table that were created 2 weeks ago. My method looks like this: def getRecent(from: Instant): Future[Seq[User]] = { db.run( users.filter(f => f.status != 0 && f.createdAt > from ).sortBy(_.id.desc).result ) } I have an implicit already that I thought would work but I guess it is only for reading rows: class UsersTable(tag: Tag) extends Table[User](tag, "users") { implicit val dateColumnType = MappedColumnType.base[Instant, Timestamp]( i => Timestamp.from(i

Slick DBIO sequence failing to compile

自闭症网瘾萝莉.ら 提交于 2019-12-12 00:54:30
问题 I am trying to save model ProductCategory object in database. While saving it, categoriesId is a Seq . case class ProductCategory(productItemId: ProductItemId, categoryies: CategoryId, filterName: FilterName) /*Inside another object starts*/ def saveCategoriesId(productItemId: ProductItemId, categoryId: Seq[CategoryId], filterName: FilterName): Future[Seq[ProductItemId]] = db.run({ DBIO.sequence(categoryId.map(id => save(ProductCategory(productItemId, id, filterName)))) }) def save

How one should handle incremental database schema evolution

99封情书 提交于 2019-12-11 23:48:30
问题 I have a Play framework powered application with database as a persistance layer (and I use Slick for that). I have enabled evolutions, generated 1.sql file and successfully rolled it out to production. Client requests new features that require database schema modifications - ie. adding new tables, adding new columns and changes to existing columns' nullability. Once all Slick's Table definitions and related code are updated, I generate schema once again and place it as 2.sql . Evolutions are

Trying to make a HCons List CaseClassShape

喜夏-厌秋 提交于 2019-12-11 23:46:38
问题 Okay, so I want to create a HListCaseClassShape that will let me create case classes that overcome the 22 arity limit. So, starting from Stefan Zeiger's code here. final class HListShape[Level <: ShapeLevel, M <: HList, U <: HList : ClassTag, P <: HList](val shapes: Seq[Shape[_, _, _, _]]) extends MappedScalaProductShape[Level, HList, M, U, P] { def buildValue(elems: IndexedSeq[Any]) = elems.foldRight(HNil: HList)(_ :: _) def copy(shapes: Seq[Shape[_ <: ShapeLevel, _, _, _]]) = new HListShape

Slick MTable.getTables always fails with Unexpected exception[JdbcSQLException: Invalid value 7 for parameter columnIndex [90008-60]]

最后都变了- 提交于 2019-12-11 20:45:30
问题 I have written this very simple code object PersonDAO { val db = Database.forConfig("h2mem1") val people = TableQuery[People] def checkTable() : Boolean = { val action = MTable.getTables val future = db.run(action) val retVal = future map {result => result map {x => x} } val x = Await.result(retVal, Duration.Inf) if (x.length > 0) { true } else { false } } } However this always fails with error message play.api.UnexpectedException: Unexpected exception[JdbcSQLException: Invalid value 7 for