1

我正在尝试使用 hasura API 在模式“abc”中将计算字段添加到我的 graphql 表“用户”,但收到以下错误:

**Saving computed field failed**
in table "abc.user": in computed field "allaccounts": function "abc.get_accounts" 
is overloaded. Overloaded functions are not supported

该功能已正确添加:

  CREATE OR REPLACE FUNCTION abc.get_accounts(id bigint)
    RETURNS VARCHAR immutable AS $$
DECLARE
    value VARCHAR;
BEGIN
    SELECT array_to_string(ARRAY_AGG( name ORDER BY name ASC )::varchar[], ',', '')
           into value
    FROM abc.account
             INNER JOIN abc.user_account ON (account.id=user_account.account_id)
    where user_account.user_id = id group by user_id;
    return value;
END;
$$ LANGUAGE plpgsql;

id 字段存在于用户表中。

我可以从下拉列表中选择函数“get_accounts”,但在添加计算字段时出现错误。任何指导表示赞赏。谢谢你。

4

2 回答 2

1

你的函数重载了。也就是说,您(至少)在数据库中有另一个使用相同名称和另一个参数列表的函数。

您通常需要识别冲突的功能,然后将其删除(除非您有充分的理由不这样做)。您可以使用以下查询展示“同音词”,该查询会生成drop function您可以直接使用的语句:

select format('drop %s %s;', case when proisagg then 'aggregate' else 'function' end, oid::regprocedure) as stmt
from pg_catalog.pg_proc
where proname = 'get_accounts'

在这个 SO 答案中归功于 Erwin Brandstetter ,其中详细介绍了如何解决问题。

于 2020-10-10T00:26:20.910 回答
1

添加 STABLE 对我有用:

CREATE OR REPLACE FUNCTION abc.all_accounts(user_row abc.user)
  RETURNS VARCHAR AS $$
DECLARE
  value VARCHAR;
BEGIN
  SELECT array_to_string(ARRAY_AGG( name ORDER BY name ASC )::varchar[], ',', '')
      into value
               FROM abc.account
                      INNER JOIN abc.user_account ON (account.id=user_account.account_id)
                   where abc.user_account.user_id = user_row.id
               group by user_id;
  return value;
END;
$$ LANGUAGE plpgsql STABLE;
于 2020-10-12T16:14:12.177 回答