I was able to store boolean values as 0 or 1 , but suddenly it keeps returning me the error
column \"trial\" is of type boolean but expression is of type integer
Im using postgresql 13 with laravel 8
how can I solve this ?
I was able to store boolean values as 0 or 1 , but suddenly it keeps returning me the error
column \"trial\" is of type boolean but expression is of type integer
Im using postgresql 13 with laravel 8
how can I solve this ?
If you are on PHP 7.4.18 or 8.0.5 then watch for a breaking change:
From Github PHP #6801:
In summary, previously if you had a boolean column on a Postgres table, you could bind an integer value (0 or 1 typically) in the where condition against that column using PDO::PARAM_INT and everything was fine. However, you will now receive an error.
While the fix was already reverted, we need to downgrade to 7.4.16 / 8.0.3 or wait for the next patch release. See PHP Issue #80892
As I am not sure about your model because you did not share it... If you READ THE DOCUMENTATION you will find out that you can cast
a value.
So, your model should have a property $casts
like this:
protected $casts = [
'trial' => 'boolean'
];
This will allow you to input any value and it will always cast it back to boolean so you can do $model->trial = 1
, or $model->trial = '1'
or $model->trial = 2
or any true
-ish value and it will cast it to true
, so you should not have any more problems...
Same goes for false
-ish values like 0
or null
, etc.
I use this. It's worked for me.
'pgsql'
=> [
'driver'
=> 'pgsql'
,
'url'
=> env('DATABASE_URL'
),
'host'
=> env('DB_HOST'
, '127.0.0.1'
),
'port'
=> env('DB_PORT'
, '5432'
),
'database'
=> env('DB_DATABASE'
, 'forge'
),
'username'
=> env('DB_USERNAME'
, 'forge'
),
'password'
=> env('DB_PASSWORD'
, ''
),
'charset'
=> 'utf8'
,
'prefix'
=> 'tbt_'
,
'prefix_indexes'
=> true,
'schema'
=> 'public'
,
'sslmode'
=> 'prefer'
,
'options'
=> extension_loaded('pdo_pgsql'
) ? array_filter([
PDO::ATTR_EMULATE_PREPARES => false,
]) : [],
],
As far as I remember, you could store int in a bool column in postgres, I tried it on my several projects. Maybe you need to alter your trial
column using:
ALTER TABLE mytable ALTER COLUMN trial DROP DEFAULT;
ALTER TABLE mytable ALTER trial TYPE bool USING CASE WHEN trial=0 THEN FALSE ELSE TRUE END;
ALTER TABLE mytable ALTER COLUMN trial SET DEFAULT FALSE;
or
ALTER TABLE mytable ALTER COLUMN trial DROP DEFAULT;
ALTER TABLE mytable ALTER trial TYPE bool USING trial::boolean;
ALTER TABLE mytable ALTER COLUMN trial SET DEFAULT FALSE;
another way is just convert integer to boolean using php:
$t = 1;
$f = 0;
$b = !!t; //true
$a = !!f; //false
SOLVED
after a lot of searching here and there , I found a query to update the pg_cast to allow 0 and 1 as boolean
update pg_cast set castcontext='a' where casttarget = 'boolean'::regtype;