Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 605 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Postgres Alter Column Integer to Boolean

#1
I've a field that is INTEGER NOT NULL DEFAULT 0 and I need to change that to bool.

This is what I am using:

ALTER TABLE mytabe
ALTER mycolumn TYPE bool
USING
CASE
WHEN 0 THEN FALSE
ELSE TRUE
END;

But I am getting:

ERROR: argument of CASE/WHEN must be type boolean, not type integer

********** Error **********

ERROR: argument of CASE/WHEN must be type boolean, not type integer
SQL state: 42804


Any idea?


Thanks.
Reply

#2
Try this:

ALTER TABLE mytabe ALTER COLUMN mycolumn DROP DEFAULT;
ALTER TABLE mytabe ALTER mycolumn TYPE bool USING CASE WHEN mycolumn=0 THEN FALSE ELSE TRUE END;
ALTER TABLE mytabe ALTER COLUMN mycolumn SET DEFAULT FALSE;


You need to remove the constraint first (as its not a boolean), and secondly your `CASE` statement was syntactically wrong.
Reply

#3
Postgres can automatically cast integer to boolean. The key phrase is

using some_col_name::boolean
-- here some_col_name is the column you want to do type change

Above Answer is correct that helped me Just one modification instead of case I used type casting

ALTER TABLE mytabe ALTER COLUMN mycolumn DROP DEFAULT;
ALTER TABLE mytabe ALTER mycolumn TYPE bool USING mycolumn::boolean;
ALTER TABLE mytabe ALTER COLUMN mycolumn SET DEFAULT FALSE;
Reply

#4
Also check that you don't have any CHECK constraint on you column like:

`[...] CONSTRAINT blabla CHECK ((field = ANY (ARRAY[0, 1])))`

otherwise you alter command will error with a `cannot convert to boolean type`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through