Insert if not exists in Slick 3.2.0

27 views
Skip to first unread message

techlan...@gmail.com

unread,
Jul 10, 2017, 12:02:58 PM7/10/17
to Slick / ScalaQuery
i'm write this code for Insert if record not exists :
def insertIfNotExists(productInput: LocaldomainRow): Future[LocaldomainRow] = {
    val productAction = (
      selectQ.filter(_.LocaldomainId===productInput.LocaldomainId).result.headOption.flatMap {
        case Some(product) =>
          mylog("product was there: " + product)
          DBIO.successful(product)
        case None =>
          mylog("inserting product")
          (selectQ returning selectQ.map(_.LocaldomainId)
            into ((prod,LocaldomainId) => prod.copy(LocaldomainId=LocaldomainId))) += LocaldomainRow(
            0,
            productInput.LocaldomainDomain,
            productInput.LocaldomainDescription,
            productInput.LocaldomainName
          )
      }
      ).transactionally      
      result=db.run(productAction)
  }
}
but not work and cannot add record ? 


Richard Dallaway

unread,
Jul 10, 2017, 1:50:11 PM7/10/17
to Slick / ScalaQuery
Hello

When you say it doesn't work, do you mean compile error, something at runtime?  If it runs, can you see what SQL is executed?  What value does your future hold when it completes?

Thanks
Richard

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/4a41f9c5-0a99-4295-9f4f-e8db887f7a45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

techlan...@gmail.com

unread,
Jul 10, 2017, 2:00:52 PM7/10/17
to Slick / ScalaQuery
val f=LocalDomains.insertIfNotExists(man)
f.onComplete{
     case Success(ok)=>println(ok)
     case Failure(ex)=>println(ex)
}
i call  insertIfNotExists like this but not print any in my console ? how can saw  sql command execute ? 

Richard Dallaway

unread,
Jul 10, 2017, 4:48:35 PM7/10/17
to Slick / ScalaQuery
One thought: perhaps your program is terminating before the future has completed.

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.

techlan...@gmail.com

unread,
Jul 11, 2017, 1:00:33 PM7/11/17
to Slick / ScalaQuery
yes that's right my  program is terminating before the future has completed.  How can i solve the problem (excuse me because i'm beginning in Scala and Slick ) 

Richard Dallaway

unread,
Jul 11, 2017, 4:02:48 PM7/11/17
to scala...@googlegroups.com
On Tue, 11 Jul 2017 at 18:00 <techlan...@gmail.com> wrote:
yes that's right my  program is terminating before the future has completed.  How can i solve the problem

It's not a Slick-specific thing, but you can wait on a Future if you absolutely have to:

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

val result = Await.result(db.run(your-database-acton-here), 4.seconds)

...but I guess lots of people will be using web frameworks or similar that handle the Future side of things. But it is useful to wait in demo programs.

(excuse me because i'm beginning in Scala and Slick ) 

Welcome! Hope you find it fun.

Regards
Richard
--

 


On Tuesday, July 11, 2017 at 1:18:35 AM UTC+4:30, Richard Dallaway wrote:
One thought: perhaps your program is terminating before the future has completed.

On Mon, 10 Jul 2017 at 19:00 <techlan...@gmail.com> wrote:
val f=LocalDomains.insertIfNotExists(man)
f.onComplete{
     case Success(ok)=>println(ok)
     case Failure(ex)=>println(ex)
}
i call  insertIfNotExists like this but not print any in my console ? how can saw  sql command execute ? 

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.

Justin du coeur

unread,
Jul 11, 2017, 4:28:10 PM7/11/17
to Slick / ScalaQuery
Just to clarify the rules of thumb: in general, don't use Await *inside* your program -- it's a good way to mess up your threading, and can really break Play, Akka, Slick, and stuff like that.

But it's not unusual to use it at the *edges* of the program, especially for testing: it lets you say "wait at least x seconds for things to finish up".  Basically, when you are "outside" your program logic, it is sometimes necessary.  This sounds like one of those cases...

To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+unsubscribe@googlegroups.com.

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/CAF_XbzDS-DT4v93-gQ_%3DgR47EvkSHUdoFo0ghknk4sRZx2YmTg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages