[GENERAL] Best way to allow column to initially be null? - Mailing list pgsql-general

From Glen Huang
Subject [GENERAL] Best way to allow column to initially be null?
Date
Msg-id 0780AF5A-AE89-4255-9857-EC3EF91570A3@gmail.com
Whole thread Raw
Responses Re: [GENERAL] Best way to allow column to initially be null?  (Berend Tober <btober@computer.org>)
List pgsql-general
Hi,

I’m trying to make a column have these properties:

1. When a row is inserted, this column is allowed to be null.
2. When the row is updated, no null can be assigned to it this column.

I initially thought I can drop the not null constraint before insertion and turn it back on after that, but after
readingthe doc it seems turning on not null constraint requires not columns contain null value, so looks like it won’t
work.

My current approach is to not set the not null constraint in the table and use a before update trigger to manually
raiseexception when the column is null. But it doesn’t seem as elegant. 

Is there a better way?

Regards,
Glen

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

pgsql-general by date:

Previous
From: Andy Colson
Date:
Subject: Re: [GENERAL] PG 10 and perl
Next
From: Berend Tober
Date:
Subject: Re: [GENERAL] Best way to allow column to initially be null?