The book states the following:
The main difference for domains compared with enums is that the
NOT NULL
constraint portion is part of the domain.
This is incorrect. The domain has a CHECK
constraint for valid non-NULL
values, but the NOT NULL
constraint is part of the column declaration and can be removed independently of the domain, as follows:
owner@localhost:5432 rideshare_development# ALTER TABLE vehicles ALTER COLUMN status DROP NOT NULL;
ALTER TABLE
owner@localhost:5432 rideshare_development# UPDATE vehicles SET status = NULL;
UPDATE 4
owner@localhost:5432 rideshare_development# SELECT DISTINCT(status) from vehicles;
status
--------
(1 row)
owner@localhost:5432 rideshare_development# \d vehicles
Table "rideshare.vehicles"
Column | Type | Collation | Nullable | Default
------------+--------------------------------+-----------+----------+--------------------------------------
id | bigint | | not null | nextval('vehicles_id_seq'::regclass)
name | character varying | | not null |
created_at | timestamp(6) without time zone | | not null |
updated_at | timestamp(6) without time zone | | not null |
status | vehicle_statuses | | | 'draft'::text
Indexes:
"vehicles_pkey" PRIMARY KEY, btree (id)
"index_vehicles_on_name" UNIQUE, btree (name)
Referenced by:
TABLE "vehicle_reservations" CONSTRAINT "fk_rails_7edc8e666a" FOREIGN KEY (vehicle_id) REFERENCES vehicles(id)
owner@localhost:5432 rideshare_development# \dD vehicle_statuses
List of domains
Schema | Name | Type | Collation | Nullable | Default | Check
-----------+------------------+------+-----------+----------+---------+---------------------------------------------------------------
rideshare | vehicle_statuses | text | | | | CHECK (VALUE = ANY (ARRAY['draft'::text, 'published'::text]))
(1 row)