sql - Create array of custom domain postgres -
because of inherit limitations of enum (you can't add values enum within function), i'm switching custom domains check constraint verifying values. need able create arrays of custom enums, when try this:
create domain foo text check (value in ('foo', 'bar')); create table foo_table(foo_column foo[]); i error
type "foo[]" not exist doing googling, found this 2004 made support coming. there way this?
thanks!
update
i've come hacky solution, i'll put answer if no 1 comes better solution in few days. solution means can't reuse type array, have create separate type acts array:
create domain foo_group text[] check (value <@ array['foo', 'bar']); create table foo_table(foo_column foo_group); the following work:
insert foo_table values(array['foo']); insert foo_table values(array['foo', 'bar']); insert foo_table values(array['bar']); the following don't:
insert foo_table values(array['foo', 'baz']); insert foo_table values(array['baz']);
another possible workaround is:
create type foo_tup (item foo); domain types can wrapped in tuples , gives array constructor. downside want create casts:
select array[row('foo')::foo_tup, row('bar')]; for example create function , cast:
create function foo_tup(foo) returns foo_tup language sql $$ select row($1)::foo_tup; $$ immutable; create function foo(foo_tup) returns foo language sql $$ select $1.item; $$; create cast (foo foo_tup) function foo_tup(foo); create cast (foo_tup foo) function foo(foo_tup); then aggregation becomes easy:
select array_agg(myfoo::foo_tup) my_table; though parentheses.
Comments
Post a Comment