PostgreSQL:未定义的函数array_agg(enumlabel)

时间:2015-10-21 21:30:17

标签: php arrays postgresql yii2

我是PostgreSQL的新手。 当我在托管上应用迁移时,我看到一个错误:

SQLSTATE[42883]: Undefined function: 7
ERROR: function array_agg(name) does not exist
LINE 12: array_to_string((select array_agg(enumlabel) from pg_enum...

我的托管有PostgreSQL 8.3.11。

正在执行的SQL:

SELECT
    d.nspname AS table_schema,
    c.relname AS table_name,
    a.attname AS column_name,
    t.typname AS data_type,
    a.attlen AS character_maximum_length,
    pg_catalog.col_description(c.oid, a.attnum) AS column_comment,
    a.atttypmod AS modifier,
    a.attnotnull = false AS is_nullable,
CAST(pg_get_expr(ad.adbin, ad.adrelid) AS varchar) AS column_default,
coalesce(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval',false) AS is_autoinc,

array_to_string((select array_agg(enumlabel)
from pg_enum
where enumtypid=a.atttypid)::varchar[],',') as enum_values,

CASE atttypid
    WHEN 21 /*int2*/ THEN 16
    WHEN 23 /*int4*/ THEN 32
    WHEN 20 /*int8*/ THEN 64
    WHEN 1700 /*numeric*/ THEN
CASE WHEN atttypmod = -1
THEN null
ELSE ((atttypmod - 4) >> 16) & 65535
END
    WHEN 700 /*float4*/ THEN 24 /*FLT_MANT_DIG*/
    WHEN 701 /*float8*/ THEN 53 /*DBL_MANT_DIG*/
ELSE null
END AS numeric_precision,
CASE
    WHEN atttypid IN (21, 23, 20) THEN 0
    WHEN atttypid IN (1700) THEN
CASE
    WHEN atttypmod = -1 THEN null
ELSE (atttypmod - 4) & 65535
END
ELSE null
END AS numeric_scale,
CAST(information_schema._pg_char_max_length(information_schema._pg_truetypid(a, t),
information_schema._pg_truetypmod(a, t)) AS numeric) AS size,
a.attnum = any (ct.conkey) as is_pkey
FROM
pg_class c
    LEFT JOIN pg_attribute a ON a.attrelid = c.oid
    LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
    LEFT JOIN pg_type t ON a.atttypid = t.oid
    LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
    LEFT join pg_constraint ct on ct.conrelid=c.oid and ct.contype='p'
WHERE
    a.attnum > 0 and t.typname != ''
    and c.relname = 'migration'
    and d.nspname = 'public'
ORDER BY
    a.attnum;
.../vendor/yiisoft/yii2/db/Schema.php:628

中的

3 个答案:

答案 0 :(得分:5)

Like commented,您最好的选择是更新到Postgres的当前版本。 Postgres 8.3中没有array_agg()函数。

然而,您可以使用基本的ARRAY constructor代替,因为至少Postgres 7.4并且碰巧对此更有效,甚至在目前的Postgres中。用您的问题替换查询中查询的粗体部分:

array_to_string(ARRAY(
   SELECT enumlabel FROM pg_enum
   WHERE  enumtypid = a.atttypid)::text[], ',') AS enum_values,

在Postgres 9.0或更高版本中,您可以使用string_agg()进行简化:

(SELECT string_agg(enumlabel, ',') FROM pg_enum
 WHERE  enumtypid = a.atttypid) AS enum_values,

简单的ARRAY构造函数应该仍然更快 顺便说一下,pg_enum本身是在Postgres 8.3(适用于enum类型)中引入的。

答案 1 :(得分:1)

PostgreSQL 8.3中没有array_agg函数。请参阅release notes

答案 2 :(得分:1)

Postgresql 8.3中没有array_agg

但是如果你不能升级,那么你可以像这样简单地创建array_agg函数:

CREATE AGGREGATE array_agg (
    sfunc = array_append,
    basetype = anyelement,
    stype = anyarray,
    initcond = '{}'
);

与问题没有直接关系:

如果您选择从8.x升级到9.x版本,一些(写得不好的)查询将不再起作用...特别是那些具有隐式转换的查询:

select * from my_table where my_int_column = some_text_value

所以,你必须重写它们,添加显式的强制转换:

select * from my_table where my_int_column = some_text_value::int

或者你可以创建一些“autocasts”,以便在你重写它们时让它们保持工作:

CREATE CAST (varchar AS integer) WITH INOUT AS IMPLICIT;
CREATE CAST (character AS integer) WITH INOUT AS IMPLICIT;