constraints on composite types

Started by Roman Neuhauserover 20 years ago8 messagesgeneral
Jump to latest
#1Roman Neuhauser
neuhauser@sigpipe.cz

This fails on 8.0.3 (syntax error at or near "." at character):

CREATE TYPE ct AS (
foo INTEGER,
bar INTEGER
);

CREATE TABLE t1 (
attr ct,
CONSTRAINT uq UNIQUE (attr.foo)
);

Should it be possible? From reading
http://www.postgresql.org/docs/current/static/rowtypes.html it looks
like almost everything else works.

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

#2Richard Huxton
dev@archonet.com
In reply to: Roman Neuhauser (#1)
Re: constraints on composite types

Roman Neuhauser wrote:

This fails on 8.0.3 (syntax error at or near "." at character):

CREATE TYPE ct AS (
foo INTEGER,
bar INTEGER
);

CREATE TABLE t1 (
attr ct,
CONSTRAINT uq UNIQUE (attr.foo)
);

Should it be possible? From reading
http://www.postgresql.org/docs/current/static/rowtypes.html it looks
like almost everything else works.

You might get somewhere with:

CREATE OR REPLACE FUNCTION testfunc(ct) RETURNS int AS
'SELECT $1.foo;'
LANGUAGE SQL IMMUTABLE;

CREATE UNIQUE INDEX t1_b_uniq ON t1 (testfunc(b));

Seems to work on 8.1beta - haven't tried on version 8, but if the syntax
is accepted I don't see why not.
--
Richard Huxton
Archonet Ltd

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Richard Huxton (#2)
Re: constraints on composite types

Richard Huxton <dev@archonet.com> writes:

You might get somewhere with:

CREATE OR REPLACE FUNCTION testfunc(ct) RETURNS int AS
'SELECT $1.foo;'
LANGUAGE SQL IMMUTABLE;

CREATE UNIQUE INDEX t1_b_uniq ON t1 (testfunc(b));

The point is that "attr.foo" is an expression, not a column name, and
the SQL spec allows UNIQUE and PRIMARY KEY only on bare column names.
I don't believe you need the function -- this should be enough:

CREATE UNIQUE INDEX t1_b_uniq ON t1 ((attr.foo));

regards, tom lane

#4Richard Huxton
dev@archonet.com
In reply to: Tom Lane (#3)
Re: constraints on composite types

Tom Lane wrote:

Richard Huxton <dev@archonet.com> writes:

You might get somewhere with:

CREATE OR REPLACE FUNCTION testfunc(ct) RETURNS int AS
'SELECT $1.foo;'
LANGUAGE SQL IMMUTABLE;

CREATE UNIQUE INDEX t1_b_uniq ON t1 (testfunc(b));

The point is that "attr.foo" is an expression, not a column name, and
the SQL spec allows UNIQUE and PRIMARY KEY only on bare column names.
I don't believe you need the function -- this should be enough:

CREATE UNIQUE INDEX t1_b_uniq ON t1 ((attr.foo));

I got: Relation "attr" does not exist (on 8.1 beta)

--
Richard Huxton
Archonet Ltd

#5Michael Fuhr
mike@fuhr.org
In reply to: Tom Lane (#3)
Re: constraints on composite types

On Fri, Sep 09, 2005 at 10:39:58AM -0400, Tom Lane wrote:

I don't believe you need the function -- this should be enough:

CREATE UNIQUE INDEX t1_b_uniq ON t1 ((attr.foo));

I was expecting that to work too, but it doesn't:

ERROR: relation "attr" does not exist

--
Michael Fuhr

#6Roman Neuhauser
neuhauser@sigpipe.cz
In reply to: Michael Fuhr (#5)
Re: constraints on composite types

# mike@fuhr.org / 2005-09-09 09:10:30 -0600:

On Fri, Sep 09, 2005 at 10:39:58AM -0400, Tom Lane wrote:

I don't believe you need the function -- this should be enough:

CREATE UNIQUE INDEX t1_b_uniq ON t1 ((attr.foo));

I was expecting that to work too, but it doesn't:

ERROR: relation "attr" does not exist

The manual says something to the effect of (table.col).subcol,
I'll need that schema-qualified as well, IOW (schema.table.col).subcol

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Richard Huxton (#4)
Re: constraints on composite types

Richard Huxton <dev@archonet.com> writes:

Tom Lane wrote:

I don't believe you need the function -- this should be enough:

CREATE UNIQUE INDEX t1_b_uniq ON t1 ((attr.foo));

I got: Relation "attr" does not exist (on 8.1 beta)

Sorry, make that

CREATE UNIQUE INDEX t1_b_uniq ON t1 (((attr).foo));

regards, tom lane

#8Michael Fuhr
mike@fuhr.org
In reply to: Roman Neuhauser (#6)
Re: constraints on composite types

On Fri, Sep 09, 2005 at 05:20:58PM +0200, Roman Neuhauser wrote:

# mike@fuhr.org / 2005-09-09 09:10:30 -0600:

On Fri, Sep 09, 2005 at 10:39:58AM -0400, Tom Lane wrote:

I don't believe you need the function -- this should be enough:

CREATE UNIQUE INDEX t1_b_uniq ON t1 ((attr.foo));

I was expecting that to work too, but it doesn't:

ERROR: relation "attr" does not exist

The manual says something to the effect of (table.col).subcol,
I'll need that schema-qualified as well, IOW (schema.table.col).subcol

This works:

CREATE UNIQUE INDEX t1_b_uniq ON t1 (((attr).foo));

--
Michael Fuhr