Re: Casting Integer to Boolean in assignment

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Alexandre GRAIL <postgresql(dot)general(at)augure(dot)net>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Casting Integer to Boolean in assignment
Date: 2019-01-24 14:28:51
Message-ID: CAKFQuwZXYgDxDLkB4a2jVn+bcuyxEWyROoKL8z-fwfrbS1pFgA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Hellmuth Vargas 2019-01-24 14:43:11 Re: How duplicate values inserted into the primary key column of table and how to fix it
Previous Message bhargav kamineni 2019-01-24 14:23:43 log_min_duration_statement