开发者

Cast to user-defined data type in PostgreSQL

开发者 https://www.devze.com 2023-01-06 23:58 出处:网络
开发者_如何转开发I have created a data type called id which consists of two text values: id(text, text)

开发者_如何转开发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);


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 ;


You have to tell PostgreSQL how to cast, CREATE CAST

0

精彩评论

暂无评论...
验证码 换一张
取 消