2

I have created a data type called id which consists of two text values:

id(text, text)

I now need to cast values to this data type before they are inserted into my table. How would I go about doing this?

I created the type as follows:

CREATE TYPE ID AS(id text, source text);
1
  • how did you create the data type? kindly show in question Commented Jul 9, 2010 at 7:41

3 Answers 3

4

Well, to create a cast you need a function that takes a value of one type as your input and outputs the type you wish to cast to (in this case "ID" - which I would name a little more verbose if I were you). What type do you want to cast from?

Realize without messing with all that, you should be able to use your type according to this page.

Just.. SELECT ROW('foo','bar')::ID ;

Sign up to request clarification or add additional context in comments.

Comments

3

You have to tell PostgreSQL how to cast, CREATE CAST

1 Comment

So I need to create a function to do the cast for me? How exactly would I go about this?
0

if we are talking about "user defined types" which are realy compatible with each other, so you can cast the type to text first and then to your custom type admin_action::text::"UserAction"

in my case "admin_action" was of type "AdminAction" and couldn't be converted to "UserAction" directly, but I've done it through "text" step.

Comments

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.