pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Check constraints do not seem to be working!!!
10+ messages / 7 participants
[nested] [flat]

* Check constraints do not seem to be working!!!
@ 2020-11-11 06:44 Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 08:38 ` Re: Check constraints do not seem to be working!!! Chris Sterritt <chris.sterritt@yobota.xyz>
  2020-11-11 08:48 ` Re: Check constraints do not seem to be working!!! Nikolay Samokhvalov <samokhvalov@gmail.com>
  0 siblings, 2 replies; 10+ messages in thread

From: Jitendra Loyal @ 2020-11-11 06:44 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Consider this table definition:
         create table t ( i serial, b bool, c bool,
                                constraint b_c check ( (b = true and c is
not null ) or (b is distinct
from true and c is null) )
                                constraint b_c check ( (b = true and c is
not null ) or (b = false and c
is null) or (b is null and c is null) )
                                );
Despite the above two constraints, the following rows get into the table:
     insert into t (b , c) values (null, true), (null, false);

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
@ 2020-11-11 08:38 ` Chris Sterritt <chris.sterritt@yobota.xyz>
  2020-11-11 08:41   ` Re: Check constraints do not seem to be working!!! Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 1 reply; 10+ messages in thread

From: Chris Sterritt @ 2020-11-11 08:38 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


On 11/11/2020 06:44, Jitendra Loyal wrote:
> Consider this table definition:
>  create table t ( i serial, b bool, c bool,
>                       constraint b_c check ( (b = true and c is not 
> null ) or (b is distinct
> from true and c is null) )
>                       constraint b_c check ( (b = true and c is not 
> null ) or (b = false and c
> is null) or (b is null and c is null) )
>                       );
> Despite the above two constraints, the following rows get into the table:
>  insert into t (b , c) values (null, true), (null, false);


(b =TRUE AND c IS NOT NULL) evaluates to null when b is null

Cheers,
Chris Sterritt

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 08:38 ` Re: Check constraints do not seem to be working!!! Chris Sterritt <chris.sterritt@yobota.xyz>
@ 2020-11-11 08:41   ` Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Pavel Stehule @ 2020-11-11 08:41 UTC (permalink / raw)
  To: Chris Sterritt <chris.sterritt@yobota.xyz>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

st 11. 11. 2020 v 9:38 odesílatel Chris Sterritt <chris.sterritt@yobota.xyz>
napsal:

>
> On 11/11/2020 06:44, Jitendra Loyal wrote:
>
> Consider this table definition:
>          create table t ( i serial, b bool, c bool,
>                                 constraint b_c check ( (b = true and c is
> not null ) or (b is distinct
> from true and c is null) )
>                                 constraint b_c check ( (b = true and c is
> not null ) or (b = false and c
> is null) or (b is null and c is null) )
>                                 );
> Despite the above two constraints, the following rows get into the table:
>      insert into t (b , c) values (null, true), (null, false);
>
>
> (b = TRUE AND c IS NOT NULL) evaluates to null when b is null
>
>
yes, constraint is violated only when result is false, no when it is null.

Regards

Pavel

> Cheers,
> Chris Sterritt
>
>

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
@ 2020-11-11 08:48 ` Nikolay Samokhvalov <samokhvalov@gmail.com>
  2020-11-11 09:06   ` Re: Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  1 sibling, 1 reply; 10+ messages in thread

From: Nikolay Samokhvalov @ 2020-11-11 08:48 UTC (permalink / raw)
  To: Jitendra Loyal <jitendra.loyal@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

On Wed, Nov 11, 2020 at 12:26 AM Jitendra Loyal <jitendra.loyal@gmail.com>
wrote:

> Despite the above two constraints, the following rows get into the table:
>      insert into t (b , c) values (null, true), (null, false);
>

This behavior is described in the docs
https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS
:

> It should be noted that a check constraint is satisfied if the check
expression evaluates to true or the null value. Since most expressions will
evaluate to the null value if any operand is null, they will not prevent
null values in the constrained columns. To ensure that a column does not
contain null values, the not-null constraint described in the next section
can be used.

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 08:48 ` Re: Check constraints do not seem to be working!!! Nikolay Samokhvalov <samokhvalov@gmail.com>
@ 2020-11-11 09:06   ` Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 10:47     ` Re: Check constraints do not seem to be working!!! Tomas Vondra <tomas.vondra@enterprisedb.com>
  2020-11-11 10:48     ` Re: Check constraints do not seem to be working!!! Alban Hertroys <haramrae@gmail.com>
  0 siblings, 2 replies; 10+ messages in thread

From: Jitendra Loyal @ 2020-11-11 09:06 UTC (permalink / raw)
  To: Nikolay Samokhvalov <samokhvalov@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

Thanks Nikolay

I read that but is there a way to meet the above requirement. And I will
like to add that IS NULL and IS NOT NULL should evaluate to true/false.
These operators are made for this and should not be returning NULL.

Regards,
Jitendra

On Wed 11 Nov, 2020, 14:18 Nikolay Samokhvalov, <samokhvalov@gmail.com>
wrote:

> On Wed, Nov 11, 2020 at 12:26 AM Jitendra Loyal <jitendra.loyal@gmail.com>
> wrote:
>
>> Despite the above two constraints, the following rows get into the table:
>>      insert into t (b , c) values (null, true), (null, false);
>>
>
> This behavior is described in the docs
> https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS
> :
>
> > It should be noted that a check constraint is satisfied if the check
> expression evaluates to true or the null value. Since most expressions will
> evaluate to the null value if any operand is null, they will not prevent
> null values in the constrained columns. To ensure that a column does not
> contain null values, the not-null constraint described in the next section
> can be used.
>

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 08:48 ` Re: Check constraints do not seem to be working!!! Nikolay Samokhvalov <samokhvalov@gmail.com>
  2020-11-11 09:06   ` Re: Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
@ 2020-11-11 10:47     ` Tomas Vondra <tomas.vondra@enterprisedb.com>
  2020-11-11 10:58       ` Re: Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 17:22       ` Re: Check constraints do not seem to be working!!! Laurenz Albe <laurenz.albe@cybertec.at>
  1 sibling, 2 replies; 10+ messages in thread

From: Tomas Vondra @ 2020-11-11 10:47 UTC (permalink / raw)
  To: Jitendra Loyal <jitendra.loyal@gmail.com>; Nikolay Samokhvalov <samokhvalov@gmail.com>; +Cc: pgsql-general@lists.postgresql.org


On 11/11/20 10:06 AM, Jitendra Loyal wrote:
> Thanks Nikolay
> 
> I read that but is there a way to meet the above requirement. And I will
> like to add that IS NULL and IS NOT NULL should evaluate to true/false.
> These operators are made for this and should not be returning NULL.
> 

This has nothing to do with IS [NOT] NULL, it's the first part of the
expression (b = TRUE) causing trouble. Essentially, the constraint

    (b = true) and (c is not null)

is evaluated in two steps. First we evaluate the two parts individually,
and for (null, true) the results would look like this:

    (b = true) => null
    (c is not null) => true

and then we combine those results using 'AND'

    null AND true => null

which is considered as if the constraint matches. If you want to handle
NULL for the first expression, you may do this, for example:

    (b it not null and b = true) and (c is not null)

Or something like that.


regards

-- 
Tomas Vondra
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company





^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 08:48 ` Re: Check constraints do not seem to be working!!! Nikolay Samokhvalov <samokhvalov@gmail.com>
  2020-11-11 09:06   ` Re: Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 10:47     ` Re: Check constraints do not seem to be working!!! Tomas Vondra <tomas.vondra@enterprisedb.com>
@ 2020-11-11 10:58       ` Jitendra Loyal <jitendra.loyal@gmail.com>
  1 sibling, 0 replies; 10+ messages in thread

From: Jitendra Loyal @ 2020-11-11 10:58 UTC (permalink / raw)
  To: Tomas Vondra <tomas.vondra@enterprisedb.com>; +Cc: Nikolay Samokhvalov <samokhvalov@gmail.com>; pgsql-general@lists.postgresql.org

Thanks Tomas....

Understood... My bad.... Was just not looking at that aspect

Thanks once again,
Regards,
Jitendra


On Wed, 11 Nov 2020 at 16:17, Tomas Vondra <tomas.vondra@enterprisedb.com>
wrote:

>
> On 11/11/20 10:06 AM, Jitendra Loyal wrote:
> > Thanks Nikolay
> >
> > I read that but is there a way to meet the above requirement. And I will
> > like to add that IS NULL and IS NOT NULL should evaluate to true/false.
> > These operators are made for this and should not be returning NULL.
> >
>
> This has nothing to do with IS [NOT] NULL, it's the first part of the
> expression (b = TRUE) causing trouble. Essentially, the constraint
>
>     (b = true) and (c is not null)
>
> is evaluated in two steps. First we evaluate the two parts individually,
> and for (null, true) the results would look like this:
>
>     (b = true) => null
>     (c is not null) => true
>
> and then we combine those results using 'AND'
>
>     null AND true => null
>
> which is considered as if the constraint matches. If you want to handle
> NULL for the first expression, you may do this, for example:
>
>     (b it not null and b = true) and (c is not null)
>
> Or something like that.
>
>
> regards
>
> --
> Tomas Vondra
> EnterpriseDB: http://www.enterprisedb.com
> The Enterprise PostgreSQL Company
>

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 08:48 ` Re: Check constraints do not seem to be working!!! Nikolay Samokhvalov <samokhvalov@gmail.com>
  2020-11-11 09:06   ` Re: Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 10:47     ` Re: Check constraints do not seem to be working!!! Tomas Vondra <tomas.vondra@enterprisedb.com>
@ 2020-11-11 17:22       ` Laurenz Albe <laurenz.albe@cybertec.at>
  2020-11-11 18:16         ` Re: Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  1 sibling, 1 reply; 10+ messages in thread

From: Laurenz Albe @ 2020-11-11 17:22 UTC (permalink / raw)
  To: Tomas Vondra <tomas.vondra@enterprisedb.com>; Jitendra Loyal <jitendra.loyal@gmail.com>; Nikolay Samokhvalov <samokhvalov@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

On Wed, 2020-11-11 at 11:47 +0100, Tomas Vondra wrote:
> you may do this, for example:
> 
>     (b it not null and b = true) and (c is not null)
> 
> Or something like that.

My (equivalent) suggestion:

   b IS TRUE AND c IS NOT NULL

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 08:48 ` Re: Check constraints do not seem to be working!!! Nikolay Samokhvalov <samokhvalov@gmail.com>
  2020-11-11 09:06   ` Re: Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 10:47     ` Re: Check constraints do not seem to be working!!! Tomas Vondra <tomas.vondra@enterprisedb.com>
  2020-11-11 17:22       ` Re: Check constraints do not seem to be working!!! Laurenz Albe <laurenz.albe@cybertec.at>
@ 2020-11-11 18:16         ` Jitendra Loyal <jitendra.loyal@gmail.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Jitendra Loyal @ 2020-11-11 18:16 UTC (permalink / raw)
  To: Laurenz Albe <laurenz.albe@cybertec.at>; +Cc: Tomas Vondra <tomas.vondra@enterprisedb.com>; Nikolay Samokhvalov <samokhvalov@gmail.com>; pgsql-general@lists.postgresql.org

Thanks Laurenz

This is interesting...b is True

Thanks and regards,
Jitendra

On Wed 11 Nov, 2020, 22:52 Laurenz Albe, <laurenz.albe@cybertec.at> wrote:

> On Wed, 2020-11-11 at 11:47 +0100, Tomas Vondra wrote:
> > you may do this, for example:
> >
> >     (b it not null and b = true) and (c is not null)
> >
> > Or something like that.
>
> My (equivalent) suggestion:
>
>    b IS TRUE AND c IS NOT NULL
>
> Yours,
> Laurenz Albe
> --
> Cybertec | https://www.cybertec-postgresql.com
>
>

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Check constraints do not seem to be working!!!
  2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
  2020-11-11 08:48 ` Re: Check constraints do not seem to be working!!! Nikolay Samokhvalov <samokhvalov@gmail.com>
  2020-11-11 09:06   ` Re: Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
@ 2020-11-11 10:48     ` Alban Hertroys <haramrae@gmail.com>
  1 sibling, 0 replies; 10+ messages in thread

From: Alban Hertroys @ 2020-11-11 10:48 UTC (permalink / raw)
  To: Jitendra Loyal <jitendra.loyal@gmail.com>; +Cc: Nikolay Samokhvalov <samokhvalov@gmail.com>; pgsql-general@lists.postgresql.org


On 11 Nov 2020, at 11:15, Jitendra Loyal <jitendra.loyal@gmail.com> wrote:
> 
> 
> Thanks Nikolay
> 
> I read that but is there a way to meet the above requirement. And I will like to add that IS NULL and IS NOT NULL should evaluate to true/false. These operators are made for this and should not be returning NULL.

That is exactly what they do. Your problem is with the equality operator and its behaviour with NULL values, which is described in the referenced document.

--
If you can't see the forest for the trees,
Cut the trees and you'll find there is no forest.

>> On Wed 11 Nov, 2020, 14:18 Nikolay Samokhvalov, <samokhvalov@gmail.com> wrote:
>>> On Wed, Nov 11, 2020 at 12:26 AM Jitendra Loyal <jitendra.loyal@gmail.com> wrote:
>>> Despite the above two constraints, the following rows get into the table:
>>>      insert into t (b , c) values (null, true), (null, false);  
>> 
>> This behavior is described in the docs https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS:
>> 
>> > It should be noted that a check constraint is satisfied if the check expression evaluates to true or the null value. Since most expressions will evaluate to the null value if any operand is null, they will not prevent null values in the constrained columns. To ensure that a column does not contain null values, the not-null constraint described in the next section can be used.

^ permalink  raw  reply  [nested|flat] 10+ messages in thread


end of thread, other threads:[~2020-11-11 18:16 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-11-11 06:44 Check constraints do not seem to be working!!! Jitendra Loyal <jitendra.loyal@gmail.com>
2020-11-11 08:38 ` Chris Sterritt <chris.sterritt@yobota.xyz>
2020-11-11 08:41   ` Pavel Stehule <pavel.stehule@gmail.com>
2020-11-11 08:48 ` Nikolay Samokhvalov <samokhvalov@gmail.com>
2020-11-11 09:06   ` Jitendra Loyal <jitendra.loyal@gmail.com>
2020-11-11 10:47     ` Tomas Vondra <tomas.vondra@enterprisedb.com>
2020-11-11 10:58       ` Jitendra Loyal <jitendra.loyal@gmail.com>
2020-11-11 17:22       ` Laurenz Albe <laurenz.albe@cybertec.at>
2020-11-11 18:16         ` Jitendra Loyal <jitendra.loyal@gmail.com>
2020-11-11 10:48     ` Alban Hertroys <haramrae@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox