Skip to content

Instantly share code, notes, and snippets.

@jimathyp
Created October 20, 2022 05:34
Show Gist options
  • Save jimathyp/923c9ec8cd9352c2bf7f21bce7b6d95e to your computer and use it in GitHub Desktop.
Save jimathyp/923c9ec8cd9352c2bf7f21bce7b6d95e to your computer and use it in GitHub Desktop.

Postgres enums

ERROR: when adding an enum to a table - column "my_column" contains null values

CREATE TYPE my_enum AS ENUM ('value1', 'value2');
ALTER TABLE some_table ADD COLUMN my_new_column my_enum NOT NULL;

ERROR:  column "my_new_column" contains null values
  • had existing data
  • new column is not null
  • hack workaround - remove not-null constraint
  • real fix: add a default value to the column
ALTER TABLE some_table ADD COLUMN my_new_column my_enum NOT NULL DEFAULT 'value1';
  • that will take time (adds value to existing rows)
  • then remove the default
ALTER TABLE some_table ALTER COLUMN my_new_column DROP DEFAULT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment