In a script for setting up a database which uses random UUIDs for primary keys, I have:
CREATE TABLE alpha (id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), value INT);
CREATE TABLE beta (aref UUID REFERENCES alpha (id));
INSERT INTO alpha (value) VALUES (42);
Then over a foreign key, to have that record in "alpha" be the default target for insertions into the "beta" table table:
DO $$
DECLARE l_id UUID;
BEGIN
SELECT alpha.id FROM alpha INTO l_id;
ALTER TABLE beta ALTER COLUMN aref SET DEFAULT l_id;
END;
$$ LANGUAGE plpgsql;
ERROR: column "l_id" does not exist CONTEXT: SQL statement "ALTER TABLE beta ALTER COLUMN aref SET DEFAULT l_id" PL/pgSQL function inline_code_block line 1 at SQL statement
How do I use the value of a variable as the default value for a column?
execute format('ALTER TABLE beta ALTER COLUMN aref SET DEFAULT %L::uuid', l_id);
eval
-style answer, but I'm happy to accept that as the answer though if you post it as such.