UPSERT *not* INSERT or REPLACE

Assuming three columns in the table: ID, NAME, ROLE BAD: This will insert or replace all columns with new values for ID=1: INSERT OR REPLACE INTO Employee (id, name, role) VALUES (1, ‘John Foo’, ‘CEO’); BAD: This will insert or replace 2 of the columns… the NAME column will be set to NULL or the … Read more

Return rows from INSERT with ON CONFLICT without needing to update

It’s the recurring problem of SELECT or INSERT, related to (but different from) an UPSERT. The new UPSERT functionality in Postgres 9.5 is still instrumental. WITH ins AS ( INSERT INTO names(name) VALUES (‘bob’) ON CONFLICT ON CONSTRAINT names_name_key DO UPDATE SET name = NULL WHERE FALSE — never executed, but locks the row RETURNING … Read more

How to resolve SQL0418N Error

Basically, DB2 doesn’t know what data types you’re sending in on those parameters. I’m guessing you’re either on an older version of DB2 (less than 9.7 on Linux/Unix/Windows, or on a Mainframe version older than 10.1), which doesn’t do a whole lot of “automatic” type conversion. Or you’re sending in NULL values (which still have … Read more

How to do an upsert with SqlAlchemy?

SQLAlchemy does have a “save-or-update” behavior, which in recent versions has been built into session.add, but previously was the separate session.saveorupdate call. This is not an “upsert” but it may be good enough for your needs. It is good that you are asking about a class with multiple unique keys; I believe this is precisely … Read more