3

I read the answer. How can I do same for bunch insert?

I have list of items, and I insert these items:

tbl ++= items

each item is just Item(id:String, text:String), id is primary key.

I want to insert only not existed items in table tbl using one sql query.

Slick 3, postgresql

Community
  • 1
  • 1
sheh
  • 1,003
  • 1
  • 9
  • 31
  • One way is try retrieving all ID's belonging to your elements from the table. That would tell you which ones are already present. You can use this info to divide your sequence into two parts: new and updated. New ones you can insert as a bulk then. – Ashalynd Dec 30 '15 at 08:52
  • @Ashalynd, I thought about it, but my program can write `items` simultaneously. I had a lot problems with slick and I replaced slick+postgresql on mongodb+http://reactivemongo.org/. Thanks for advice! – sheh Dec 30 '15 at 09:10
  • I see. From the point of performance, I'd rather stay with Postgres, but if it works for you, it's cool. – Ashalynd Dec 30 '15 at 14:40

1 Answers1

0

You can try to do it as:

  1. Check which entries exists in database
  2. Filter them out
  3. Bulk insert

Code sample:

def markAsNew(list: Seq[IssueEvent]): Future[Option[Int]] = { val ids = list.map(_.originEventId)).map(_.originEventId) dbRun((for { existing <- events.filter(_.originEventId inSet ids).result filtered = list.filter(event => existing.contains(event.originEventId)) count <- events ++= filtered } yield count).transactionally) }

Klapsa2503
  • 829
  • 10
  • 33