e.g.
db withSession {
implicit session: Session =>
val records = result.videos.map(v => videoToRecord(queryTerm, v))
records foreach {
record =>
if (!videos.filter(_.videoId === record.videoId).exists.run)
videos += record
}
queryResults += YtQueryResultRecord(queryTerm, result.timePeriods, currentTimestamp)
}
I still get exceptions thrown for duplicates - see below. I guess Slick is using two operations for the insert? I have multiple threads doing inserts - is there anyway around this without using locking locally in the client?
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "video_id_idx"
Detail: Key (video_id)=(W3n4JTF_cNs) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:363)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:1423)
at scala.slick.driver.JdbcInvokerComponent$BaseInsertInvoker$$anonfun$internalInsert$1.apply(JdbcInvokerComponent.scala:115)
at scala.slick.driver.JdbcInvokerComponent$BaseInsertInvoker$$anonfun$internalInsert$1.apply(JdbcInvokerComponent.scala:112)
at scala.slick.jdbc.JdbcBackend$SessionDef$class.withPreparedStatement(JdbcBackend.scala:161)
at scala.slick.jdbc.JdbcBackend$BaseSession.withPreparedStatement(JdbcBackend.scala:297)
at scala.slick.driver.JdbcInvokerComponent$BaseInsertInvoker.prepared(JdbcInvokerComponent.scala:101)
at scala.slick.driver.JdbcInvokerComponent$BaseInsertInvoker.internalInsert(JdbcInvokerComponent.scala:112)
at scala.slick.driver.JdbcInvokerComponent$BaseInsertInvoker.insert(JdbcInvokerComponent.scala:104)
at scala.slick.driver.JdbcInvokerComponent$BaseInsertInvoker.$plus$eq(JdbcInvokerComponent.scala:151)