Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Filipe Teixeira
    @te-x

    Hey, I'm using slick to write to an sqlite database, but I keep getting a warning and would like to get rid of it:

    val dbUrl = s"jdbc:sqlite:${outputFile.toString}"
    val db = Database.forURL(dbUrl)

    gives me a Having maxConnection > maxThreads can result in deadlocks if transactions or database locks are used.

    Is it ok to just set maxConnections to 1: val db = Database.forURL(dbUrl, executor = AsyncExecutor.default("AsyncExecutor.default", 1))
    I'm not sure what are the performance impacts of this
    Mohamedali10
    @Mohamedali10
    Hello , How to avoid the following warning from appearence:
    12:33:31,935 INFO  [application] Creating Pool for datasource 'default'
    12:33:31,987 INFO  [application] not starting JMS bridge in dev or test mode
    12:33:32,041 INFO  [application] Creating Pool for datasource 'default'
    12:33:32,094 INFO  [application] not starting JMS bridge in dev or test mode
    Gavin Bisesi
    @Daenyth
    @te-x The warning is correct, you are at risk of deadlocks. Set maxConnections == maxThreads
    evbo
    @evbo

    I really love Slick's mapTo functionality for converting query results into case classes. What about a reuse situation where I want to mapTo a case class but omit some fields to define later on (e.g. using copy)?

    I would need to specify None in my query map for the columns I don't want specified yet. For now I'm working around this by having 2 case classes, and I only mapTo the one with fewer fields

    e.g.: .map(_ => (_.a, _.b, None).mapTo[SomeCC])
    Is something like that supported?
    Usage example would be queries that are easier to maintain if broken up into separate queries, but you'd like a summarized type that contains the results from each separate query. So in one query only some columns are being fetched.
    Gavin Bisesi
    @Daenyth
    You can use <> instead
    .mapTo[CC] is ~= <> ((CC.apply _).tupled, CC.unapply)
    We use the more verbose form for some custom mapping, eg (Rep[Option[A]], Rep[Option[B]]) => Rep[Either[A, B]] for some classes
    evbo
    @evbo
    thanks!
    evbo
    @evbo
    @Daenyth the only extra step is I needed a custom unapply, which is expected since I'm mapping a partial set of CC fields
    ShankarShastri
    @shankarshastri
    Hello guys,
    I was using slick to connect to DB, the database object created, will it create a connection to the database when created or how does it actually work?
    Can someone help me on this one, either a doc or explanation will suffice.
    Heikki Vesalainen
    @hvesalai
    Depends on the configuration
    If you have a DB pool that pre-creates connections, then they will be created upon instantiation (see min pool size and max pool size)
    If, on the other hand, you don't use any pooling, then connections are only created when needed (and discarded after that -- which can be costly)
    ShankarShastri
    @shankarshastri
    Thanks @hvesalai
    Cory Knapp
    @cmknapp

    I have the following situation: We work with prices as a case class Price(currency: Currency, amount: Int), and in the database we store these as two separate columns, using a LiftedPrice in * mappings. We want to have price be optional in certain situations, so in the actual db table, we have two nullable fields---currency and amount---, while our internal representation is price: Option[Price]. We want to be able to extract values from these two fields and combine them to something like Rep[Option[Price]].

    In our code, we have a case class LiftedPriceOpt(currency: Rep[Option[Currency]], amount: Rep[Option[Int]]), with an implicit CaseClassShape((LiftedPriceOpt.apply _).tupled, (Price.fromOptions _).tupled). This compiles, and it works when we build a lifted case class out of a query result, but it doesn't work in * mappings---we get a runtime exception which looks like it's because slick is trying to access fields of an option when inserting.

    I'm guessing that we really shouldn't be using CaseClassShape here, but I'm not sure how to build a Shape for Option[Price]. How should I be going about this?

    Gavin Bisesi
    @Daenyth
    I'd use <> in your * to define the mapping manually
    case class Product(name: String, id: Long, price: Option[Price])
    object Price {
      val build: (Option[Currency], Option[Int]) => Option[Price] = {
        case (Some(cur), Some(amt)) => Some(Price(cur, amt))
        case _ => None
      }
      val extract: Price => (Currency, Int) = p => (p.currency, p.amount)
    }
    def * = (
      itemName,
      itemId,
      (priceCurrency, priceAmount) <> (Price.build, _.map(Price.extract)),
    ).mapTo[Product]
    something like that
    Cory Knapp
    @cmknapp
    That's a cleaner use of <> than what we have, thanks. But I asked the wrong question, I guess: We have some standard "slices" of the table that we work with in queries to control which columns can be updated from different sources---e.g., for your example we might have a case class ProductSlice(name: String, price: Option[Price]), and we need a lifted version like LiftedProductSlice(name: Rep[String], price: Rep[Option[Price]]). Is it possible to get a function ProductTable => LiftedProductSlice? If we had a Shape[_, A, Price, A], then we can use this type A in place of Rep[Option[Price]]. Alternatively, I can flatten this LiftedProductSlice, but then I still need a type mapper for my slice---presumably I can use <> again, but I'm not seeing how to.
    Gavin Bisesi
    @Daenyth
    You can use <> inside of Query#map
    CaseClassShape is appropriate for Query usage, if you find yourself using the same one multiple times
    query.map { row =>
      (
        row.name,
        (row.priceCurrency, row.priceAmount) <> (..Price..)
      )
    }
    but if you use the lifted rep you could do
    query.map { row =>
      LiftedProductSlick(row.name, LiftedPrice(row.priceCurrency, row.priceAmount))
    }
    Cory Knapp
    @cmknapp
    Right. That last thing is what I want to do, but with a LiftedOptionalPrice---but CaseClassShape doesn't seem to match what we're doing with our Lifted shape---because Option[Price] is not a case class.
    Gavin Bisesi
    @Daenyth
    ahh right
    I'm not sure how to get LiftedPrice to present itself as Rep[Option[Price]]
    Cory Knapp
    @cmknapp
    I feel like all I need is a Shape[Something, Option[Price], LiftedOptionalPrice] that is actually correct. We have a CaseClassShape, which has "worked" so far, but only because we haven't used it to do writes.
    I tried to extend Shape by hand, but my toNode is obviously incorrect, and I have no idea what it should be
    Cory Knapp
    @cmknapp
    In case anyone is interested, I created an implicit conversion between Option[Price] and an OptionalPrice which allows me to define a CaseClassShape with LiftedPriceOpt. In the ProductSlice above, I have to replace Option[Price] with OptionalPrice or slick fails to map the shapes---but everything just works.
    2efper
    @2efPer
    How can I ADD FOREIGN KEY REFERENCES to this table itself's column?
    Taeguk Kwon
    @taeguk
    Excuse me, I have a question.
    Is there a way to do bulk insertion in plain query?
    I want to insert random sized objects to database using only one insert statement.
    If the size is fixed, I can easily do that with just writing plain query.
    But if the size is random, I don't know how to do.
    Any one can help me?
    Francisco Albert Albusac
    @tatitati

    People, in my domain (made with case classes), I want to add something similar to this:

    trait SurrogateId[T] {
      self: T =>
      def setSurrogateId(withSurrogateId: Option[Long]): T = {
        self.copy(surrogateId = withSurrogateId)
      }
    }

    But is not working obviously....is there any standard approach for this out there?

    sanjarbek
    @sanjarbek
    Hi guys. I need you help. How to import implicits get and set java.time.LocalDate in plain sql?
    Gavin Bisesi
    @Daenyth
    @tatitati a typeclass approach
    trait HasSurrogateId[T] {
      def setSurrogateId(obj: T, id: Option[Long]): T
    }
    you can use implicit syntax on top of that
    and if you put the implementation in the companion object for each type then no imports needed
    Francisco Albert Albusac
    @tatitati
    o.....I have to try that idea, thanks @Daenyth
    Ali Akhtar
    @AliAkhtar__twitter
    prog20901
    @prog20901
    any open source app or framework exist to build an app - write once, same code and run on PC, Web and Mobile ???