I've recently started working with PostgreSQL and stumbled upon a problem with their UPSERT implementation.
Basically, I want to store an id -> name mapping in a table, with the usual characteristics.
CREATE TABLE entities (id SERIAL PRIMARY KEY, name VARCHAR(10) UNIQUE NOT NULL);
Then, I want to issue a query (or queries) with some entity names that would return their indices, inserting them if necessary.It appears that's impossible to do with PostgreSQL. The closest I can get is this:
INSERT INTO entities (name) VALUES ('a'), ('b'), ('c')
ON CONFLICT (name) DO NOTHING
RETURNING id, name;
However, this (1) only returns the newly inserted rows, and (2) it increments the `id` counter even in case of conflicts (i.e. if the name already exists). While (2) is merely annoying, (1) means I have to issue at least one more SELECT query.We like to mock Java, C++ and Go for being stuck in the past, but that's nothing compared to the state of SQL. Sure, there are custom extensions that each database provider implements, but they're often lacking in obvious ways. I really wish there was some significant progress, e.g. TypeSQL or CoffeeSQL or something.