2

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..

1
  • 1
    Why obfuscate such a simple thing as a text array with a custom type? Commented Dec 2, 2019 at 19:06

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Klin, this helps.. Its automated conversion of thousands of objects from Oracle. just picking the failed ones :)
I see. Did you think about a domain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.