some_user
some_user

Reputation: 325

Postgres cast Array to a Custom Type

I have to cast a varchar array to a custom Type but it is failing.

Scenario

CREATE TYPE foo AS (foo text[]);

SELECT ARRAY['TEST_ONE']::foo; -- fails with ERROR: cannot cast type text[] to foo

I actually have to pass this type as an optional parameter to a function and hence I have to place its default value in the function parameters list. Like this

create function foo_func(par1 foo DEFAULT ARRAY['TEST_ONE']::foo) .... but this doesn't work due to above mentioned issue...

Help will be much appreciated..

Upvotes: 2

Views: 2035

Answers (1)

klin
klin

Reputation: 121919

It is hard to guess why someone would want to complicate his life with such a strange idea. Anyway, the type foo is a composite type with a single text[] element, so the literal should look like:

SELECT ROW(ARRAY['TEST_ONE'])::foo;

Maybe a domain would be more handy:

create domain foo as text[];
select array['test_one']::foo;

Upvotes: 2

Related Questions