Sample Header Ad - 728x90

Insert data into multiple tables connected by foreign key PostgreSQL

0 votes
1 answer
4892 views
I have two tables which are linked by a foreign key.
CREATE TABLE clients (
    id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name character varying,
    phone character varying,
    contact text
);
CREATE TABLE address (
    id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    line1 character varying,
    city character varying,
    state text,
    zip character varying,
    uuid integer REFERENCES clients(id) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED
);
My lambda server receives data which should be put into both tables. However the queries I am trying to run are inserting a NULL value into the uuid foreign key column of my address table.
BEGIN; 
INSERT INTO clients (name, phone, contact) 
VALUES ('Trevor J','6077730',''); 

INSERT INTO address (line1, city, state, zip) 
VALUES ('82 Haon Street,'Gasto','OH', 51113); 
COMMIT;
What am I missing to get the proper foreign key in my table?
Asked by tdammon (115 rep)
Sep 10, 2022, 03:13 PM
Last activity: Aug 6, 2025, 05:00 AM