Search Postgresql Archives

Re: Casting Integer to Boolean in assignment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jan 24, 2019 at 4:04 AM Alexandre GRAIL
<postgresql.general@xxxxxxxxxx> 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.




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux