hugsql

erwinrooijakkers 2019-06-13T13:28:29.012Z

Hi

erwinrooijakkers 2019-06-13T13:29:07.012900Z

I am working with PostgreSQL and wanting to INSERT multiple rows at once with an ON CONFLICT DO UPDATE statement

erwinrooijakkers 2019-06-13T13:29:59.013100Z

I’ve got something like this:

-- :name add-things! :! :n
INSERT INTO my_table (
  p,
  foo
)
VALUES :tuple*:values
ON CONFLICT (p) DO UPDATE
SET my_table.foo = foo
where p is the primary key. I call this with: (add-things! {:values [[1 1] [2 3]]})

erwinrooijakkers 2019-06-13T13:30:01.013400Z

But this returns:

erwinrooijakkers 2019-06-13T13:30:27.013700Z

org.postgresql.util.PSQLException: ERROR: column reference "foo" is ambiguous

erwinrooijakkers 2019-06-13T13:30:42.013900Z

Any idea how to fix?

cjmurphy 2019-06-13T13:35:21.015200Z

Try putting prefixing foo with a colon.

erwinrooijakkers 2019-06-13T13:35:30.015400Z

Okay

erwinrooijakkers 2019-06-13T13:36:03.015700Z

Thanks

erwinrooijakkers 2019-06-13T13:36:17.016Z

That results in

clojure.lang.ExceptionInfo: Parameter Mismatch: :foo parameter data not found.

cjmurphy 2019-06-13T13:38:40.017500Z

You have to say what :foo is, so it has to be a key in the map you provide when you call add-things.

cjmurphy 2019-06-13T13:39:49.018100Z

You have a key for :values, but also need one for :foo.

cjmurphy 2019-06-13T13:41:59.019400Z

You should also make sure the SQL you want to generate is valid. Isn't SET normally used with an UPDATE statement?

cjmurphy 2019-06-13T14:40:45.020900Z

Oh I see - it is an insert statement that falls back to an update statement. I think your question got answered in SO.

erwinrooijakkers 2019-06-13T15:08:54.021100Z

Thanks for the help

erwinrooijakkers 2019-06-13T15:23:51.021500Z

it works by using foo = EXCLUDED.foo 🙂