I have a postgres database schema where newly inserted rows reference other rows. The skeleton of the table is:
CREATE TABLE table_1 (
f1 UUID PRIMARY KEY,
f2 UUID NOT NULL,
f3 text NOT NULL,
f4 text NOT NULL DEFAULT '',
f5 text NOT NULL DEFAULT '',
f6 UUID NULL,
);
This is the INSERT query I'm using:
INSERT INTO table_a (f1, f2, f3, f4, f5, f6)
SELECT $1, $2, $3, f4, f5, f2
FROM table_a
WHERE <conditionals>
ORDER BY <ordering>
LIMIT 1
Basic idea is that I look for an existing row that satisfies the conditions. If one exists, use fields f4, f5, and f2 to fill fields f4, f5, and f6 in the new row being inserted. If one does not exist, those fields should instead take on their default values ('', '', and NULL, respectively).
Problem is, if no row is found, the SELECT itself seems to return no results, and the entire insertion is simply skipped. It works fine if a row matches the conditionals, but not if no rows are returned by that SELECT. How can I get the desired behavior?