I am coming from graph databases and postgres is still super foreign to me.
I have the following tables
CREATE TYPE runnerenum AS ENUM ('runner');
CREATE TABLE IF NOT EXISTS collections (
collectionid UUID PRIMARY KEY,
name VARCHAR(256) UNIQUE NOT NULL,
runner runnerenum NOT NULL,
runconfig JSONB
);
CREATE TABLE IF NOT EXISTS namedexprs(
namedexprid UUID PRIMARY KEY,
name VARCHAR(256) UNIQUE NOT NULL,
-- exprid UUID NOT NULL REFERENCES expressions(exprid),
collectionid UUID NOT NULL REFERENCES collections(collectionid) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS expressions(
exprid UUID PRIMARY KEY,
ast JSONB NOT NULL,
namedexprid UUID NOT NULL REFERENCES namedexprs(namedexprid) ON DELETE CASCADE
);
My question is what is the idiomatic way to create a collections
atomically (while also creating associated expressions
and namedexprs
). Currently I am executing three separate queries and getting errors because of a foreign key violation.
BEGIN; INSERT INTO collections ...; INSERT into namedexprs ...; INSERT INTO expression ...; COMMIT;
2) Look atDEFERRABLE
here CREATE TABLEDEFERRABLE
is what I need as my current trouble is a key constraint violation error. Could you provide an example of this syntax within myCREATE TABLE
example? I assume I would need these for both of myREFERENCES
clauses, something likenamedexprid UUID NOT NULL REFERENCES namedexprs(namedexprid) ON DELETE CASCADE INITIALLY DEFERRED
?