This query
INSERT INTO "event_user_notification_sent" (
"id", "ts", "notification"
) (
SELECT "id", "ts", "notification"
FROM "event_notification_sent"
WHERE "id" IN (
SELECT "id"
FROM "user_notification"
)
);
fails with
ERROR: insert or update on table "event_user_notification_sent" violates foreign key constraint "event_user_notification_sent_notification_fkey"
DETAIL: Key (notification)=(4) is not present in table "user_notification".
Let's look at the constraint in question:
snowdrift_development=# \d event_user_notification_sent;
Table "public.event_user_notification_sent" Column | Type | Modifiers
--------------+--------------------------+---------------------------------------------------------------------------
id | integer | not null default nextval('event_user_notification_sent_id_seq'::regclass)
ts | timestamp with time zone | not null
notification | bigint | not null
Indexes:
"event_user_notification_sent_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"event_user_notification_sent_notification_fkey" FOREIGN KEY (notification) REFERENCES user_notification(id)
The last line means that "each 'notification' field must correspond to the 'id' field of the 'user_notification' table.
But why does it need an 'id = 4'?
SELECT "id" FROM "user_notification";
-[ RECORD 1 ]
id | 2
-[ RECORD 2 ]
id | 3