CREATE TABLE test_fk_def1 (id SERIAL PRIMARY KEY, name TEXT);

CREATE TABLE test_fk_def2 (id SERIAL PRIMARY KEY,
  fk INTEGER REFERENCES test_fk_def1(id) 
    ON UPDATE CASCADE
    ON DELETE RESTRICT
    DEFERRABLE INITIALLY DEFERRED);

INSERT INTO test_fk_def1 (name) VALUES ('one');
INSERT INTO test_fk_def2 (fk) SELECT id FROM test_fk_def1
    ORDER BY id DESC LIMIT 1;

BEGIN;
-- this should be unnecessary since it is initially deferred, but it doesn't
-- work with or without it
SET CONSTRAINTS test_fk_def2_fk_fkey DEFERRED;
DELETE FROM test_fk_def1; -- why does this fail?
DELETE FROM test_fk_def2;
COMMIT;
