slick-2.0

How to COUNT(*) in Slick 2.0?

时间秒杀一切 提交于 2019-12-20 11:51:25
问题 According to the Slick 2.0 documentation, to get the count of rows in a table: val q1 = coffees.length // compiles to SQL (simplified): // select count(1) from "COFFEES" However, it turns out that coffees.length is of type Column[Int] . How does one execute the query and get the value? 回答1: I just had this same problem upgrading to slick 2.0. I forget where the exact method lives, but the generic .run seems to work for me, i.e. coffees.length.run 回答2: StaticQuery.queryNA[Int]("select count(*)

How to do “OR” filter in slick

老子叫甜甜 提交于 2019-12-18 19:44:40
问题 In slick we can use query.filter( m => (m.state === state1 && m.status === status1) || (m.state === state2 && m.status == status2)) for an "OR" condition in where clause. However my requirement is I have "OR" conditions in a list (passed by user as part of URL). Conditions list includes tuples of state and status like List[(state1, status1),(state2, status2),(state3, status3)] So what I wanted was to either be able to build the || statement inside of filter so that I can use each of the

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

slick 2 mapping many to one relationship with mappedColumn

喜欢而已 提交于 2019-12-11 12:44:45
问题 i'm trying to map my class with slick so i can persist them. My business objects are defined this way case class Book(id : Option[Long], author : Author, title : String, readDate : Date, review : String){} case class Author(id : Option[Long], name : String, surname : String) {} Then I defined the "table" class for authors: class Authors(tag : Tag) extends Table[Author](tag,"AUTHORS") { def id = column[Option[Long]]("AUTHOR_ID", O.PrimaryKey, O.AutoInc) def name = column[String]("NAME") def

Where can I define methods to be called on Tables?

大兔子大兔子 提交于 2019-12-11 12:39:33
问题 (I'm a complete beginner with Scala and Slick, so code review of any kind is appreciated) I have the following class and Slick Table defined: case class Foo(title: String, description: String, id: Int = 0) class FooTable(tag: Tag) extends Table[Foo](tag, "FOO") { def id = column[Int]("ID", O.PrimaryKey, O.AutoInc) def title = column[String]("TITLE", O.NotNull) def description = column[String]("DESCRIPTION") def * = (title, description, id) <> (Foo.tupled, Foo.unapply) } I want to add a method

Slick 2 aggregation - how to get a scalar result?

心已入冬 提交于 2019-12-11 11:19:50
问题 I have a table with an Int column TIME in it: def time = column[Int]("TIME") The table is mapped to a custom type. I want to find a maximum time value, i.e. to perform a simple aggregation. The example in the documentation seems easy enough: val q = coffees.map(_.price) val q1 = q.min val q2 = q.max However, when I do this, the type of q1 and q2 is Column[Option[Int]] . I can perform a get or getOrElse on this to get a result of type Column[Int] (even this seems somewhat surprising to me - is

Where to put my database access methods when using a DAO with Slick 2.0?

拟墨画扇 提交于 2019-12-11 10:24:48
问题 (This question is based on a very similar previous request for help. With the introduction of a DAO and multiple database drivers, the same problem requires a different approach, and I hope warrants a new SO question.) I have a class and Slick Table defined like this: import play.api.db.slick.Profile case class Foo(title: String, description: String, id: Int = 0) trait FooComponent extends Profile { this: Profile => import profile.simple._ class FooTable(tag: Tag) extends Table[Foo](tag, "FOO

Slick - optionally including / omitting a large column

六眼飞鱼酱① 提交于 2019-12-11 10:04:13
问题 Suppose I have a table with a number of small columns, and a large (say BLOB) column: case class Thing(id: Int, small1: String, small2: String, small3: String, large: String) class ThingMapping(tag: Tag) extends Table[Thing](tag, "things") { def id = column[Int]("id", O.PrimaryKey, O.NotNull, O.AutoInc) def small1 = column[String]("small1") def small2 = column[String]("small2") def small3 = column[String]("small3") def large = column[String]("large") def * = (id, small1, small2, small3, large

How to return multiple values after insert?

孤者浪人 提交于 2019-12-11 04:49:53
问题 It's Slick 2.0. I have the following: mytable .returning(mytable.map(_.id)) .+=(item)(session) I'd like the insert not only to return the id but also the name field and the lastname fields. Is it possible in Slick? 回答1: map determines what projection of the table is returned, so you can pick the fields you need and pack them in a tuple: mytable .returning(mytable.map(t => (t.id, t.name, t.lastname))) .+=(item)(session) 来源: https://stackoverflow.com/questions/27675813/how-to-return-multiple

Returning the auto incrementing value after an insert using slick

 ̄綄美尐妖づ 提交于 2019-12-11 03:16:01
问题 I am using slick 2.0.1 (and can upgrade if required) and I want to retrieve the auto incrementing value from the database (postgresql). I have seen a few questions on SO on this already, but they are fairly old and I was hoping there was a better way than to do what this answer suggests: Scala & Play! & Slick & PostgreSQL auto increment def autoInc = name ~ price ~ description returning id def add(product: Product)(implicit s:Session): Long = { Products.autoInc.insert(p.name, p.price, p