CTE containing ambiguous columns - Mailing list pgsql-hackers

From Robert Haas
Subject CTE containing ambiguous columns
Date
Msg-id 603c8f070911122020p273aa220hca39b7ee9d886bc8@mail.gmail.com
Whole thread Raw
Responses Re: CTE containing ambiguous columns
Re: CTE containing ambiguous columns
List pgsql-hackers
Suppose you do this:

create table animals (id serial primary key, name varchar not null);

Then you can do this:

with beings as (select * from animals) select * from beings where id = 1;

But not this:

with beings as (select * from animals a1, animals a2) select * from
beings where id = 1;

Because:

ERROR:  column reference "id" is ambiguous at character 82
STATEMENT:  with beings as (select * from animals a1, animals a2)
select * from beings where id = 1;
ERROR:  column reference "id" is ambiguous
LINE 1: ...m animals a1, animals a2) select * from beings where id = 1;
             ^
 
My email program will probably mangle this, so the error cursor here
is point to "id = 1", at the end, and saying that's ambiguous.  Which
is sorta kinda true, but the usual remedy of qualifying it with a
relation name (here, beings.id) fails.  And you can't quantify it with
a1.id or a2.id either, they're out of scope.  In some sense, the real
problem is with "select *", because that is what is expanding into a
non-unique list of column names.  But you don't actually trigger an
error unless you try to reference one; the same query works fine
without the where clause.

I'm not sure if there's anything useful we can do about this, but it
definitely threw me for a loop.

...Robert


pgsql-hackers by date:

Previous
From: KaiGai Kohei
Date:
Subject: Re: TRIGGER with WHEN clause
Next
From: Bruce Momjian
Date:
Subject: Re: next CommitFest