User defined type without single quotes

Started by Rodrigo Barbozaabout 13 years ago3 messagesdocs
Jump to latest
#1Rodrigo Barboza
rodrigombufrj@gmail.com

Hello.
I defined a new type to represent an unsigned int, but when I try do run a
query it complains because it is not quoted and casted to my type.

Suppose I have a table:

-- my_uint32 is my new type
CREATE TABLE test (a my_uin32);

If I try to run this insert, postgres complain about the type:
INSERT INTO teste (a) VALUES (10);

But this one works:
NSERT INTO teste (a) VALUES ('10'::my_uint);

Is there a way to avoid the single quotes?

#2Peter Eisentraut
peter_e@gmx.net
In reply to: Rodrigo Barboza (#1)
Re: User defined type without single quotes

On Thu, 2013-04-04 at 19:50 -0300, Rodrigo Barboza wrote:

-- my_uint32 is my new type
CREATE TABLE test (a my_uin32);

If I try to run this insert, postgres complain about the type:
INSERT INTO teste (a) VALUES (10);

But this one works:
NSERT INTO teste (a) VALUES ('10'::my_uint);

Is there a way to avoid the single quotes?

A constant like 10 is initially assigned one of the integer types (the
exact rules are in the documentation). In order to be able to store
that into a column of a custom type, you need to define a cast between
the integer type and your type with at least assignment context.

--
Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-docs

#3Rodrigo Barboza
rodrigombufrj@gmail.com
In reply to: Peter Eisentraut (#2)
Re: User defined type without single quotes

Thank you, Peter!

On Sat, Apr 13, 2013 at 10:06 AM, Peter Eisentraut <peter_e@gmx.net> wrote:

Show quoted text

On Thu, 2013-04-04 at 19:50 -0300, Rodrigo Barboza wrote:

-- my_uint32 is my new type
CREATE TABLE test (a my_uin32);

If I try to run this insert, postgres complain about the type:
INSERT INTO teste (a) VALUES (10);

But this one works:
NSERT INTO teste (a) VALUES ('10'::my_uint);

Is there a way to avoid the single quotes?

A constant like 10 is initially assigned one of the integer types (the
exact rules are in the documentation). In order to be able to store
that into a column of a custom type, you need to define a cast between
the integer type and your type with at least assignment context.