我试图在USERS / GROUPS数据模型中指示超级用户如下:
create table users (user_id int);
create table groups (group_id int, superuser boolean);
create table usergroup( user_id int, group_id int );
select users.user_id,
('t' = any( array_agg('superuser') ))
from users
left join usergroup using(user_id)
left join groups using (group_id)
group by user_id
但Postgres(9.1)告诉我:
error : ERROR: could not determine polymorphic type because input has type "unknown"
从此我得知我应该在某处添加一个明确的Cast,但我无法弄清楚在哪里。有人可以提供建议吗? 如果可能的话,我想要一个解释的答案,所以我可以从中学习
答案 0 :(得分:1)
您的查询可以像这样修复:
select users.user_id,
(true = any( array_agg(superuser) ))
from users
left join usergroup using(user_id)
left join groups using (group_id)
group by user_id
你在超级用户'上做了array_agg。 text literal,而不是超级用户列。
我会这样写:
select users.user_id,
exists (
select *
from usergroup ug, groups g
where ug.user_id = users.id
and ug.group_id = g.id
and ug.superuser
) as is_superuser
from users