Casting Integer to Boolean in assignment

From: Alexandre GRAIL <postgresql(dot)general(at)augure(dot)net>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Casting Integer to Boolean in assignment
Date: 2019-01-24 11:04:14
Message-ID: d8fe822a-ac75-5763-00b3-273f11394fc5@augure-ng.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello the list,

Maybe this question has been debated before (I didn't find anything
helpful) but :

Why the default is to throw an error when casting Integer to Boolean in
assignment, and accepting it everywhere else ?

So you can type :

postgres=# select 1::boolean;
 bool
------
 t
(1 row)

or

postgres=# select 0::boolean;
 bool
------
 f
(1 row)

But you *cannot* use 1 or 0 as valid input for boolean type when
inserting or updating :

test=# CREATE TABLE test1 (a boolean);
CREATE TABLE
test=# INSERT INTO test1 VALUES (1);
ERROR:  column "a" is of type boolean but expression is of type integer
LINE 1: INSERT INTO test1 VALUES (1);
                                  ^
HINT:  You will need to rewrite or cast the expression.

This behavior cannot be changed, as this cast is hard coded with
"Implicit?=no".

And added to this weirdness is the fact that '1' or '0' (with quote) is OK.

So is there a reason to forbid 0 and 1 as valid boolean, without
explicit cast ?

Thanks!

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Geoff Winkless 2019-01-24 11:45:54 Re: Casting Integer to Boolean in assignment
Previous Message Ron 2019-01-24 10:11:29 [pgbackrest] Expiring the last backup?