由于输入的类型为“未知",因此无法确定多态类型. [英] Could not determine polymorphic type because input has type "unknown"

查看:184
本文介绍了由于输入的类型为“未知",因此无法确定多态类型.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,输出为

由于输入的类型为未知",因此无法确定多态类型

Could not determine polymorphic type because input has type "unknown"

查询:

select ( array_to_string(array_agg(name), ', '))::text as name,path 
from(select 'fullpath' as Path,null as id,'' as name 
     from tblabc where key = 'key1' and value = '1' 
    ) as e 
group by path;

我有一个 postgres 数据库

推荐答案

此处的问题是'' as name实际上没有为值指定类型.这是unknown类型,PostgreSQL通常从诸如您要插入的列或将其传递给什么函数之类的东西推断出真实类型.

The issue here is that '' as name doesn't actually specify a type for the value. It's the unknown type, and PostgreSQL usually infers the real type from things like what column you're inserting it into or what function you pass it to.

在这种情况下,将其传递给 polymorphc 函数array_agg.它可以接受伪类型anyelement的输入,这实际上只是意味着在运行时将其格式化".

In this case, you pass it to array_agg, which is a polymorphc function. It can take inputs of the pseudo-type anyelement, which really just means "figure it out at runtime".

PostgreSQL仍然会弄清楚,除了array_to_string实际上并没有接受text[]作为输入.它需要anyarray-另一个多态类型,例如数组的anyelement.

PostgreSQL would still figure it out except that array_to_string doesn't actually take a text[] as input. It takes anyarray - another polymorphic type, like anyelement for arrays.

因此查询中没有任何内容可以告诉PostgreSQL ''是什么类型.可能会猜到您的意思是text,但这有点太挑剔了.因此,它抱怨.问题简化为:

So there's nothing in the query to tell PostgreSQL what type that '' is. It could guess you meant text, but it's a bit too fussy for that. So it complains. The issue simplifies down to:

regress=> SELECT array_to_string(array_agg(''), ',');
ERROR:  could not determine polymorphic type because input has type "unknown"

要解决此问题,请写一个带类型的文字:

To solve this, write a typed literal:

TEXT '' AS name

或使用演员表:

CAST('' AS text) AS name

或PostgreSQL的简写:

or the PostgreSQL shorthand:

''::text

示例:

regress=> SELECT array_to_string(array_agg(TEXT ''), ',');
 array_to_string 
-----------------

(1 row)

regress=> SELECT array_to_string(array_agg(''::text), ',');
 array_to_string 
-----------------

(1 row)

regress=> SELECT array_to_string(array_agg(CAST('' AS text)), ',');
 array_to_string 
-----------------

(1 row)

这篇关于由于输入的类型为“未知",因此无法确定多态类型.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆