Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info

    ```val action =
    for {
    lock <- sql"""
    select * from anything
    }yield (lock) => r.head > 0).andThen())

    How to use HikariCP when creating DB connection using forURL method ? I can't find any documentation
    I was using before forConfig for the same and that by default it used to take HikariCP. However, my requirement changed recently and have to pull DB credentials from Vault. I couldn't find anyway to pull info from Vault to fed while using forConfig method.
    @kk3009 something like that
    But for(x <- y) yield x, is essentially the same as y
    technically, => x)
    Also filter on an action just fails if it's false
    Not sure what your andThen is for

    Hi all,
    hoping to get some help. I have a column in table of type timestamp in mysql and I am trying to format it in localDateTime. The column definition in scala looks like

    val createdDate = column[LocalDateTime]("createdDate")

    It throws an exception as: java.time.format.DateTimeParseException: Text '2020-04-26 12:00:00' could not be parsed at index 10

    Richard Dallaway

    @vyshnavi-adusumelli Looking at the default format Slick uses for LocalDateTime in MySQL it seems to be 2020-04-26Z12:00:00.00 (possibly the .00 at the end is optional).

    If you want to customise the format Slick uses there, probably the easiest way is to wrap LocalDateTime in another class .

    10 replies
    Hi, is there advantage of using MySQL over postgreSQL as a database with slick? or there is no difference?
    Antoine Doeraene
    There is basically no difference. That's the promise of slick. And, in the event that you would need it, it's quite easy to define the SQL functions that would be missing from one or the other database system
    Hi, I'm using postgres 11 with slick 3.x. I'd like to use plain sql to insert some data and get back the current value of the autoincrement id for the row.
    sqlu"""insert into test (greeting) values ("Hi") returning id""".map { id => ... }
    In Postgres there is the "returning" keyword which enables just that. But I couldn't figure out how to make Slick accept that an insert statement returns a value. Slick complains: "Update statements should not return a ResultSet".
    Jason Pickens

    I’m having a hard time getting fold to work on an optional column. The definition is:

    /** Extension methods for Options of single- and multi-column values */
    final class AnyOptionExtensionMethods[O <: Rep[_], P](val r: O) extends AnyVal {
      /** Apply `f` to the value inside this Option, if it is non-empty, otherwise return `ifEmpty`. */
      def fold[B, BP](ifEmpty: B)(f: P => B)(implicit shape: Shape[FlatShapeLevel, B, _, BP]): BP = {

    The issue is that the only value for P that seems to work is Rep[T]but then f: Rep[T] => B and there is no such function.

    Jason Pickens
    For example:
    val c: Rep[Option[Long]] = b.sum
    // This is the implicit conversion that is always applied for c.fold:
    val d: AnyOptionExtensionMethods[Rep[Option[Long]], Rep[Long]] = anyOptionExtensionMethods(c)
    // This does not compile:
    val d2: AnyOptionExtensionMethods[Rep[Option[Long]], Long] = anyOptionExtensionMethods(c)
    What I want to do is:
    c.fold(0) { x =>
      if ((x) >= 0) x else 0
    The comment says "Apply f to the value inside this Option, if it is non-empty, otherwise return ifEmpty” but I just can’t get f to accept the value inside the Option.
    Richard Dallaway
    @splink rather than sqlu"..." try sql"....".as[Long] (or similar: not tried to compile that)
    Richard Dallaway
    @steinybot Hello! What SQL would you want to produce? (If you're in Rep it's happening in the database, probably)
    Jason Pickens
    That’s a good question. I guess it probably wouldn’t be possible. So then how is fold useful?
    Richard Dallaway

    I guess you can do it use SQL case expressions (to be honest, I've never really got the hang of them in SQL).

     val zero: Rep[Int] = LiteralColumn(0)
     val query: Rep[Int] = zero )(n => Case.If(n < 0).Then(zero).Else(n))

    That might do it. (Being very tentative... I'd want to see what that really does!)

    Luís Campos

    Hello :)

    Using Slick 3.3.2.

      case class Example1()
      case class Example2()
      class Table1(tag: Tag) extends Table[Example1](tag, "table1") {
        def column1 = column[String]("column1")
        def * = ???
      private val table1 = TableQuery[Table1]
      class Table2(tag: Tag) extends Table[Example2](tag, "table2") {
        def column1 = column[Option[String]]("column1")
        def * = ???
        def requester: ForeignKeyQuery[Table1, Example1] = foreignKey("", column1, table1)(_.column1)

    Shouldn't the type of requesterbe ForeignKeyQuery[Table1, Option[Example1]]since Table2's column1 is Rep[Option[String]]?

    Justin Pihony
    Following along the lines of I'm unsure if"slick.jdbc.PostgresProfile", "org.postgresql.Driver", "jdbc:postgresql://localhost/postgres", "/tmp/", "codegen", Some("postgres"), Some("password"), false, false) should generate java.time objects? It currently generates java.sql objects as I assume it's easier to go to that default. Thoughts on opening a ticket for making the generator able to spit out the java.time objects?
    Dominik Dorn
    hey! the r2dbc project managed to get their postgres-driver included into the postgresql-jdbc org on github.. are there plans to support r2dbc as backend (instead of jdbc) ? they expose a publisher, but sadly use the spring project reactor underneath..
    Loránd Szakács
    hello folks, is there a slick WIP version for dotty?
    how can i apply session parameter using slick??(without jdbc url expression) is there any example?
    Richard Dallaway
    Hi @gomeplus - there's a withStatementParameters on a action. That might be what you're after? It's mentioned on this page in the manual, and otherwise, there's always the source.
    Utkarsh Deep
    I am trying to apply join on column that is Option[Int] but as expected it is returning me nothing. How do I prohibit join if the column is None?
    @d6y i've seen it before, but i wanna apply variables to specific session such as sort_buffer_size or query_buffer_size of mysql.
    I'am using MySQL database with slick, I want to store the content of a file in the database in a specific encoding is there a difference to use a ByteString or Array of Bytes in order to do that? does slick use the default encoding in case of using ByteString despite I used the wanted encoding for creating the ByteString?
    Richard Dallaway
    @utkarshdeep maybe and a where clause to omit null columns? Something like .filter(_.column.isDefined) ?
    Richard Dallaway
    @gomeplus you can access the connection inside a SimpleDBIO (see, e.g., JDBC interop). Not sure if from there you can get to apply the settings you need. Failing that, the next place I'd look is on the Database object, but if you're avoiding the JDBC URL that's maybe not a route that will help.
    Ahmet Yavuz Demir
    Hello folks, I have a problem that I couldn't solved
    I have a case class A where one of its fields is also Sequence of case class B
    when I try to define the table of this case class A it gives me error on "No implicit arguments of Type[Type[Seq[B]]]"
    What should I do now? :(
    André Schmidt
    hello. On lagom project I create a postgres readside. But with using a composited key on each new run (tables already built) I get always a psql exception with relation already exists. My kes looks like this def idxKey = index("article_number_fulfiller_idx",(completeArticleNumber,fulfillerVendorNumber),unique = true) is their any solution for this exception?
    Justin Pihony
    :point_up: June 21, 2020 12:06 AM If nobody responds over the weekend I'll just go ahead and create an issue - please let me know if this was intentional
    Tuomas Kinnunen
    Hi, what is the best way to create a Table schema with variable typed column? My software needs to create new tables with different types dynamically during runtime
    My current implementation just uses a String, but I need to use integer and float types to optimize space usage
    Roberto Leibman
    What does this mean?
    Caused by: slick.SlickException: Cannot use node Filter s2 for inserting data
              .filter(_.userId === userWallet.userId).insertOrUpdate(
    Where the table is:
      class UserWalletTable(_tableTag: Tag) extends profile.api.Table[UserWalletRow](_tableTag, Some("chuti"), "userWallet") {
        def *  = (userId, amount) <> (UserWalletRow.tupled, UserWalletRow.unapply)
        val userId: Rep[UserId] = column[UserId]("userId", O.PrimaryKey)
        val amount: Rep[BigDecimal] = column[BigDecimal]("amount")
    Tuomas Kinnunen
    @rleibman It says that you cannot use .filter with .insertOrUpdate. I think you first need to run that select query to get a result with the primary key and then run insertOrUpdate with value having that primary key if it existed.
    Tuomas Kinnunen
    actually you already know the userId so you don't need to do a select query, just run insertOrUpdate without the filter
    Hi guys, does anything have any guides or reading material where I can see how and what the best practices are to unit test Slick DB operations?
    Tuomas Kinnunen
    Maybe try to search for other projects on github and look at how others have done tests. I have just done sequential tests which create the tables and inserts some test data to an in-memory database.
    Does anyone know how ProvenShape works? I'm trying to create a table with type parameter...
    Tuomas Kinnunen
    ok, got it working, forgot the type parameter from apply and unapply and marked it as T: BaseTypedType