Making nulls distinct in Postgres < 15 #111
thecodedrift
announced in
TIL
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
From the postgres docs on unique indexes:
Unfortunately, disabling distinct null values via
NULLS NOT DISTINCT
is a pg 15+ feature meaning it might be a while until you can use it. As a workaround, you can instead store a generated column. For example, a nullableuuid
column that must still be unique can be solved first by creating the generated columnThen the resulting
generated_column
can be used in a unique index. Thecoalesce
function lets you turn null values into a non-null value ensuring the unique index works as expected. In this caseuuid_nil()
returns a special uuid of all0
characters.Beta Was this translation helpful? Give feedback.
All reactions