This is not an uncharitable read btw. Actual quotes:
> The first thing I learned was how to insert multiple rows with a single INSERT statement: ...
> Scouring the docs I discovered the RETURNING clause of an INSERT statement.
This is not an uncharitable read btw. Actual quotes:
> The first thing I learned was how to insert multiple rows with a single INSERT statement: ...
> Scouring the docs I discovered the RETURNING clause of an INSERT statement.
That said, I was unaware of the big discovery in the post: Doing INSERT ... RETURNING ... ON CONFLICT DO NOTHING, and passing an array of rows to insert, the returning clause will return only the values from the rows that were actually inserted. IMO this is the "obvious" behavior, but it is still worthwhile to know it works!
I think this is why people love Postgres so much. 99 times out of 100, the "obvious" behaviour turns out to be the behaviour that Postgres actually implements.
All the RDBMS systems are guilty of it to some degree though.
E.g.
CREATE TABLE t (id SERIAL PRIMARY KEY, x INTEGER UNIQUE);
INSERT INTO t (x) VALUES (42), (43) ON CONFLICT DO NOTHING RETURNING *;
This does EXACTLY the most useful thing if one of '42' or '43' conflicts.In simple CRUD cases yes. In many other scenarios, you might not. If your insert is a select statement, with joins and clauses, you can easily insert too much data, or no data at all.
I often use "insert into mytable(...) select ... from ... where ...", and I know I only want one row inserted.
In such cases checking that exactly one row was inserted is a very nice safeguard against buggy code/query, or in the freak case someone messed with the DB.
That said, I'm with you on the clickbait title. That really grinds my gears.
The problem with this blog post isn’t lack of novelty; rather, with the clickbait title, it piqued my interest but I ended up learning nothing, other than being reminded of my own terrible designs and queries back when I was a noob.
I'm pretty sure your first schema designs had relations (tables). I can't speak for relationSHIPS (foreign keys) though...
;)
I second what you said about reading a text book. I read through the entire PG documentation and a PG book my second year as an engineer and 7 years later its paid off big time. It’s one of those things where if you do it early, you get compounding benefits over your career.
There's a lot of incomplete and misunderstood information floating around in blog tutorials or potboiler textbooks.
https://www.amazon.com/Joe-Celkos-SQL-Smarties-Programming/d...