Skip to content

Instantly share code, notes, and snippets.

@travisofthenorth
Last active October 18, 2017 23:11
Show Gist options
  • Save travisofthenorth/948332daead04c3da947598a9eac0ce7 to your computer and use it in GitHub Desktop.
Save travisofthenorth/948332daead04c3da947598a9eac0ce7 to your computer and use it in GitHub Desktop.
PG not-null constraint as a check
[development] # CREATE TABLE test (id integer, name text);
[development] # ALTER TABLE test ADD CHECK (name is not null) NOT VALID;
[development] # \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
name | text |
Check constraints:
"test_name_check" CHECK (name IS NOT NULL) NOT VALID
[development] # ALTER TABLE test VALIDATE CONSTRAINT "test_name_check";
[development] # INSERT INTO test VALUES (1, NULL);
ERROR: 23514: new row for relation "test" violates check constraint "test_name_check"
DETAIL: Failing row contains (1, null).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment