Re: Casting Integer to Boolean in assignment - Mailing list pgsql-general

From David G. Johnston
Subject Re: Casting Integer to Boolean in assignment
Date
Msg-id CAKFQuwZXYgDxDLkB4a2jVn+bcuyxEWyROoKL8z-fwfrbS1pFgA@mail.gmail.com
Whole thread Raw
In response to Casting Integer to Boolean in assignment  (Alexandre GRAIL <postgresql.general@augure.net>)
Responses Re: Casting Integer to Boolean in assignment
List pgsql-general
On Thu, Jan 24, 2019 at 4:04 AM Alexandre GRAIL
<postgresql.general@augure.net> wrote:
> 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.

Because '1' is the literal character 1 with an unknown type and so can
be (must be) assigned its initial real type from context.

1 is an integer, which is a real type

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

To assist developers in avoiding the writing of buggy queries.

David J.


pgsql-general by date:

Previous
From: bhargav kamineni
Date:
Subject: log_min_duration_statement
Next
From: Hellmuth Vargas
Date:
Subject: Re: How duplicate values inserted into the primary key column oftable and how to fix it