Last active
March 23, 2026 14:42
-
-
Save yahonda/aa137f2c86a83bd4468628b8b1cc1496 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #1 It should raise "ERROR: insert or update on table "child" violates foreign key constraint "child_parent_fk" but does not | |
| ```sql | |
| postgres=# select version(); | |
| version | |
| ----------------------------------------------------------------------------------------------------------------------------- | |
| PostgreSQL 18.3 (Homebrew) on aarch64-apple-darwin25.2.0, compiled by Apple clang version 17.0.0 (clang-1700.6.3.2), 64-bit | |
| (1 row) | |
| postgres=# CREATE TABLE parent ( | |
| id INTEGER PRIMARY KEY, | |
| name TEXT NOT NULL | |
| ); | |
| CREATE TABLE | |
| postgres=# CREATE TABLE child ( | |
| id INTEGER PRIMARY KEY, | |
| parent_id INTEGER NOT NULL, | |
| name TEXT NOT NULL, | |
| CONSTRAINT child_parent_fk | |
| FOREIGN KEY (parent_id) | |
| REFERENCES parent(id) | |
| ); | |
| CREATE TABLE | |
| postgres=# SELECT | |
| conname, | |
| contype, | |
| convalidated | |
| FROM pg_constraint | |
| WHERE conname = 'child_parent_fk'; | |
| conname | contype | convalidated | |
| -----------------+---------+-------------- | |
| child_parent_fk | f | t | |
| (1 row) | |
| postgres=# ALTER TABLE child DISABLE TRIGGER ALL; | |
| ALTER TABLE | |
| postgres=# INSERT INTO child VALUES (3, 999, 'orphan-child'); | |
| INSERT 0 1 | |
| postgres=# ALTER TABLE child ENABLE TRIGGER ALL; | |
| ALTER TABLE | |
| postgres=# SELECT | |
| conname, | |
| contype, | |
| convalidated | |
| FROM pg_constraint | |
| WHERE conname = 'child_parent_fk'; | |
| conname | contype | convalidated | |
| -----------------+---------+-------------- | |
| child_parent_fk | f | t | |
| (1 row) | |
| postgres=# DROP TABLE IF EXISTS child; | |
| DROP TABLE | |
| postgres=# DROP TABLE IF EXISTS parent; | |
| DROP TABLE | |
| postgres=# | |
| ``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment