Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call insertOrUpdate when pk is generated via AbstractTable.primaryKey instead of ColumnOptions.PrimaryKey #288

Closed
jozanek opened this issue Jun 28, 2016 · 3 comments

Comments

@jozanek
Copy link

jozanek commented Jun 28, 2016

My table definition:

case class Bean2(id: Option[Long], code: String, col2: Int)

class UpsertTestTable2(tag: Tag) extends Table[Bean2](tag, "test_tab_upsert2") {
    def id = column[Long]("id", O.AutoInc)
    def code = column[String]("code")
    def col2 = column[Int]("col2")
    def * = (id.?, code, col2) <> (Bean2.tupled, Bean2.unapply)
    def pk = primaryKey("pk_a", (code, col2))
}

val UpsertTests2 = TableQuery[UpsertTestTable2]

I use it like this:
UpsertTests2.insertOrUpdate(Bean2(None, "aa", 3))

Observation 1 - Query looks strange:
insert into "test_tab_upsert2" (code,col2) values (?,?) on conflict () do update set "code"=EXCLUDED."code","col2"=EXCLUDED."col2" where ? is null or ?=?

Observation 2 - Error I get:

org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
[info]   Position: 72
[info]   at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)
[info]   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)
[info]   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
[info]   at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
[info]   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420)
[info]   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:366)
[info]   at slick.driver.JdbcActionComponent$InsertActionComposerImpl$InsertOrUpdateAction$$anonfun$nativeUpsert$1.apply(JdbcActionComponent.scala:560)
[info]   at slick.driver.JdbcActionComponent$InsertActionComposerImpl$InsertOrUpdateAction$$anonfun$nativeUpsert$1.apply(JdbcActionComponent.scala:557)
[info]   at slick.jdbc.JdbcBackend$SessionDef$class.withPreparedStatement(JdbcBackend.scala:347)
[info]   at slick.jdbc.JdbcBackend$BaseSession.withPreparedStatement(JdbcBackend.scala:407)
[info]   ...

Looking at code this is place where query is wrongly composed (line 96 in ExPostgresDriver.scala):
private lazy val (pkSyms, softSyms) = insertingSyms.partition(_.options.contains(ColumnOption.PrimaryKey))

@tminglei
Copy link
Owner

@jozi-k yes, AbstractTable.primaryKey defined pk is not covered. I'll try to enhance it.

@tminglei
Copy link
Owner

tminglei commented Jul 2, 2016

@jozi-k Here I only fix it for pg native upsert implementation, which is on slick-pg side.
But I still need to fix it for pg emulating implementation and other dbs, which is on slick side.

@twistedpair
Copy link

twistedpair commented Nov 4, 2016

Note to others that happen until this ticket

While not necessarily the cause of this user's error, if you ever neglect to specify all Primary key columns in your Table class, you will get this very same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants