Re: Best way to allow column to initially be null?

From: Berend Tober <btober(at)computer(dot)org>
To: Glen Huang <heyhgl(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org >> PG-General Mailing List" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Best way to allow column to initially be null?
Date: 2017-09-30 12:51:11
Message-ID: ede5288f-77d6-8359-8f93-97b69c8841c7@computer.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Glen Huang wrote:
> 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 reading the 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 raise exception when the column is null. But it doesn’t seem as elegant.
>
> Is there a better way?
>

Sounds to me like a BEFORE UPDATE trigger is exactly the way to handle this. Rejecting invalid data
input values is an ideal use case for such a facility.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Glen Huang 2017-09-30 13:03:15 Re: Best way to allow column to initially be null?
Previous Message Glen Huang 2017-09-30 07:46:38 Best way to allow column to initially be null?