Column reference “id” is ambiguous - It could refer to either a PL/pgSQL variable or a table column
up vote
0
down vote
favorite
I have this simple test function in Postgres (in my test
schema).
CREATE OR REPLACE FUNCTION test.func_001
(
par_id int
)
RETURNS TABLE
(
id int
)
AS
$BODY$
DECLARE
var_id int;
BEGIN
update test.item --- this is a table
set
id = 4
WHERE
id = 44;
return query
select 1000 as id;
END;
$BODY$
LANGUAGE plpgsql;
The test.item
table has a single id column.
I get the error below while trying to run the function.
Query execution failed
Reason:
SQL Error [42702]: ERROR: column reference "id" is ambiguous
Detail: It could refer to either a PL/pgSQL variable or a table column.
Where: PL/pgSQL function test.func_001(integer) line 8 at SQL statement
This error seems weird, does it mean that Postgres is finding a conflict/clash between the test.item.id
column and the id
column from the returned table?!
How come? This doesn't make any sense.
I cannot believe this but I see no other id usages here.
Note that if I comment out just this part.
-- WHERE
-- id = 44;
then suddenly the function works fine.
So it seems Postgres is confusing the id in the where
clause with something else that's named id?!
With what?
This is totally illogical and counter-intuitive.
Could someone please explain?
postgresql plpgsql
add a comment |
up vote
0
down vote
favorite
I have this simple test function in Postgres (in my test
schema).
CREATE OR REPLACE FUNCTION test.func_001
(
par_id int
)
RETURNS TABLE
(
id int
)
AS
$BODY$
DECLARE
var_id int;
BEGIN
update test.item --- this is a table
set
id = 4
WHERE
id = 44;
return query
select 1000 as id;
END;
$BODY$
LANGUAGE plpgsql;
The test.item
table has a single id column.
I get the error below while trying to run the function.
Query execution failed
Reason:
SQL Error [42702]: ERROR: column reference "id" is ambiguous
Detail: It could refer to either a PL/pgSQL variable or a table column.
Where: PL/pgSQL function test.func_001(integer) line 8 at SQL statement
This error seems weird, does it mean that Postgres is finding a conflict/clash between the test.item.id
column and the id
column from the returned table?!
How come? This doesn't make any sense.
I cannot believe this but I see no other id usages here.
Note that if I comment out just this part.
-- WHERE
-- id = 44;
then suddenly the function works fine.
So it seems Postgres is confusing the id in the where
clause with something else that's named id?!
With what?
This is totally illogical and counter-intuitive.
Could someone please explain?
postgresql plpgsql
I guess you get the error, because you have a return value named id and a column in your table with the same name
– Jens
Nov 8 at 9:32
Well, OK... I don't quite have a return valueid
. I am returning a table (from this function) and that returned table has an id column. Also, I have a physical table (test.item
) which has anid
column. I am amazed why it is finding a clash between the two. Makes no sense to me. Note that I have read the full manual about PL/pgSQL, its syntax, etc. There is nothing there documenting this weird behavior.
– peter.petrov
Nov 8 at 9:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this simple test function in Postgres (in my test
schema).
CREATE OR REPLACE FUNCTION test.func_001
(
par_id int
)
RETURNS TABLE
(
id int
)
AS
$BODY$
DECLARE
var_id int;
BEGIN
update test.item --- this is a table
set
id = 4
WHERE
id = 44;
return query
select 1000 as id;
END;
$BODY$
LANGUAGE plpgsql;
The test.item
table has a single id column.
I get the error below while trying to run the function.
Query execution failed
Reason:
SQL Error [42702]: ERROR: column reference "id" is ambiguous
Detail: It could refer to either a PL/pgSQL variable or a table column.
Where: PL/pgSQL function test.func_001(integer) line 8 at SQL statement
This error seems weird, does it mean that Postgres is finding a conflict/clash between the test.item.id
column and the id
column from the returned table?!
How come? This doesn't make any sense.
I cannot believe this but I see no other id usages here.
Note that if I comment out just this part.
-- WHERE
-- id = 44;
then suddenly the function works fine.
So it seems Postgres is confusing the id in the where
clause with something else that's named id?!
With what?
This is totally illogical and counter-intuitive.
Could someone please explain?
postgresql plpgsql
I have this simple test function in Postgres (in my test
schema).
CREATE OR REPLACE FUNCTION test.func_001
(
par_id int
)
RETURNS TABLE
(
id int
)
AS
$BODY$
DECLARE
var_id int;
BEGIN
update test.item --- this is a table
set
id = 4
WHERE
id = 44;
return query
select 1000 as id;
END;
$BODY$
LANGUAGE plpgsql;
The test.item
table has a single id column.
I get the error below while trying to run the function.
Query execution failed
Reason:
SQL Error [42702]: ERROR: column reference "id" is ambiguous
Detail: It could refer to either a PL/pgSQL variable or a table column.
Where: PL/pgSQL function test.func_001(integer) line 8 at SQL statement
This error seems weird, does it mean that Postgres is finding a conflict/clash between the test.item.id
column and the id
column from the returned table?!
How come? This doesn't make any sense.
I cannot believe this but I see no other id usages here.
Note that if I comment out just this part.
-- WHERE
-- id = 44;
then suddenly the function works fine.
So it seems Postgres is confusing the id in the where
clause with something else that's named id?!
With what?
This is totally illogical and counter-intuitive.
Could someone please explain?
postgresql plpgsql
postgresql plpgsql
edited Nov 8 at 9:45
Laurenz Albe
40.9k92745
40.9k92745
asked Nov 8 at 9:29
peter.petrov
27k74881
27k74881
I guess you get the error, because you have a return value named id and a column in your table with the same name
– Jens
Nov 8 at 9:32
Well, OK... I don't quite have a return valueid
. I am returning a table (from this function) and that returned table has an id column. Also, I have a physical table (test.item
) which has anid
column. I am amazed why it is finding a clash between the two. Makes no sense to me. Note that I have read the full manual about PL/pgSQL, its syntax, etc. There is nothing there documenting this weird behavior.
– peter.petrov
Nov 8 at 9:35
add a comment |
I guess you get the error, because you have a return value named id and a column in your table with the same name
– Jens
Nov 8 at 9:32
Well, OK... I don't quite have a return valueid
. I am returning a table (from this function) and that returned table has an id column. Also, I have a physical table (test.item
) which has anid
column. I am amazed why it is finding a clash between the two. Makes no sense to me. Note that I have read the full manual about PL/pgSQL, its syntax, etc. There is nothing there documenting this weird behavior.
– peter.petrov
Nov 8 at 9:35
I guess you get the error, because you have a return value named id and a column in your table with the same name
– Jens
Nov 8 at 9:32
I guess you get the error, because you have a return value named id and a column in your table with the same name
– Jens
Nov 8 at 9:32
Well, OK... I don't quite have a return value
id
. I am returning a table (from this function) and that returned table has an id column. Also, I have a physical table (test.item
) which has an id
column. I am amazed why it is finding a clash between the two. Makes no sense to me. Note that I have read the full manual about PL/pgSQL, its syntax, etc. There is nothing there documenting this weird behavior.– peter.petrov
Nov 8 at 9:35
Well, OK... I don't quite have a return value
id
. I am returning a table (from this function) and that returned table has an id column. Also, I have a physical table (test.item
) which has an id
column. I am amazed why it is finding a clash between the two. Makes no sense to me. Note that I have read the full manual about PL/pgSQL, its syntax, etc. There is nothing there documenting this weird behavior.– peter.petrov
Nov 8 at 9:35
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
The clause FUNCTION fx() RETURNS TABLE(x int, y int, ...)
is same like FUNCTION(OUT x int, OUT y int) RETURNS SETOF record
. So in your case, there is implicit variable id
, although you don't use it explicitly.
PostgreSQL design allows to produce rows without SQL.
CREATE OR REPLACE FUNCTION foo(a int)
RETURNS TABLE(b int, c int) AS $$
BEGIN
FOR i IN 1..a
LOOP
b := i; c := i * 10;
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE plpgsql;
See following document, please.
There is another way to declare a function as returning a set, which is to use the syntax RETURNS TABLE(columns). This is equivalent to using one or more OUT parameters plus marking the function as returning SETOF record (or SETOF a single output parameter's type, as appropriate). This notation is specified in recent versions of the SQL standard, and thus may be more portable than using SETOF.
add a comment |
up vote
2
down vote
There is a name conflict between the variable id
that is defined by the RETURNS TABLE
clause and the column of the same name.
Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you rather than doing something that may not be what you intended.
Qualify the column reference like this to remove the ambiguity:
WHERE test.item.id = 44
Well, if I had a local (function level) variable, I would understand this error but not in this case... So you're saying that theid
inRETURNS TABLE(id int)
under the hood is just implemented as a variable? And that's why Postgres is saying that I have an ambiguity?
– peter.petrov
Nov 8 at 10:02
"Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you..." That's kind of exaggerated btw.
– peter.petrov
Nov 8 at 10:03
I don't thinkid
in the column list of the return table should clash with variables and column names of other tables. That doesn't sound normal, not for 2018. But anyway, thanks, I will qualify my column names as you suggest.
– peter.petrov
Nov 8 at 10:07
It is less exaggerated than what you originally had in your question. And I think it is true.
– Laurenz Albe
Nov 8 at 10:07
Your way of boldly stating that everything you don't understand is backwards gives me the impression that you don't have a lot of programming experience.
– Laurenz Albe
Nov 8 at 10:09
|
show 7 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
The clause FUNCTION fx() RETURNS TABLE(x int, y int, ...)
is same like FUNCTION(OUT x int, OUT y int) RETURNS SETOF record
. So in your case, there is implicit variable id
, although you don't use it explicitly.
PostgreSQL design allows to produce rows without SQL.
CREATE OR REPLACE FUNCTION foo(a int)
RETURNS TABLE(b int, c int) AS $$
BEGIN
FOR i IN 1..a
LOOP
b := i; c := i * 10;
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE plpgsql;
See following document, please.
There is another way to declare a function as returning a set, which is to use the syntax RETURNS TABLE(columns). This is equivalent to using one or more OUT parameters plus marking the function as returning SETOF record (or SETOF a single output parameter's type, as appropriate). This notation is specified in recent versions of the SQL standard, and thus may be more portable than using SETOF.
add a comment |
up vote
3
down vote
accepted
The clause FUNCTION fx() RETURNS TABLE(x int, y int, ...)
is same like FUNCTION(OUT x int, OUT y int) RETURNS SETOF record
. So in your case, there is implicit variable id
, although you don't use it explicitly.
PostgreSQL design allows to produce rows without SQL.
CREATE OR REPLACE FUNCTION foo(a int)
RETURNS TABLE(b int, c int) AS $$
BEGIN
FOR i IN 1..a
LOOP
b := i; c := i * 10;
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE plpgsql;
See following document, please.
There is another way to declare a function as returning a set, which is to use the syntax RETURNS TABLE(columns). This is equivalent to using one or more OUT parameters plus marking the function as returning SETOF record (or SETOF a single output parameter's type, as appropriate). This notation is specified in recent versions of the SQL standard, and thus may be more portable than using SETOF.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The clause FUNCTION fx() RETURNS TABLE(x int, y int, ...)
is same like FUNCTION(OUT x int, OUT y int) RETURNS SETOF record
. So in your case, there is implicit variable id
, although you don't use it explicitly.
PostgreSQL design allows to produce rows without SQL.
CREATE OR REPLACE FUNCTION foo(a int)
RETURNS TABLE(b int, c int) AS $$
BEGIN
FOR i IN 1..a
LOOP
b := i; c := i * 10;
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE plpgsql;
See following document, please.
There is another way to declare a function as returning a set, which is to use the syntax RETURNS TABLE(columns). This is equivalent to using one or more OUT parameters plus marking the function as returning SETOF record (or SETOF a single output parameter's type, as appropriate). This notation is specified in recent versions of the SQL standard, and thus may be more portable than using SETOF.
The clause FUNCTION fx() RETURNS TABLE(x int, y int, ...)
is same like FUNCTION(OUT x int, OUT y int) RETURNS SETOF record
. So in your case, there is implicit variable id
, although you don't use it explicitly.
PostgreSQL design allows to produce rows without SQL.
CREATE OR REPLACE FUNCTION foo(a int)
RETURNS TABLE(b int, c int) AS $$
BEGIN
FOR i IN 1..a
LOOP
b := i; c := i * 10;
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE plpgsql;
See following document, please.
There is another way to declare a function as returning a set, which is to use the syntax RETURNS TABLE(columns). This is equivalent to using one or more OUT parameters plus marking the function as returning SETOF record (or SETOF a single output parameter's type, as appropriate). This notation is specified in recent versions of the SQL standard, and thus may be more portable than using SETOF.
answered Nov 8 at 10:22
Pavel Stehule
21.8k34855
21.8k34855
add a comment |
add a comment |
up vote
2
down vote
There is a name conflict between the variable id
that is defined by the RETURNS TABLE
clause and the column of the same name.
Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you rather than doing something that may not be what you intended.
Qualify the column reference like this to remove the ambiguity:
WHERE test.item.id = 44
Well, if I had a local (function level) variable, I would understand this error but not in this case... So you're saying that theid
inRETURNS TABLE(id int)
under the hood is just implemented as a variable? And that's why Postgres is saying that I have an ambiguity?
– peter.petrov
Nov 8 at 10:02
"Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you..." That's kind of exaggerated btw.
– peter.petrov
Nov 8 at 10:03
I don't thinkid
in the column list of the return table should clash with variables and column names of other tables. That doesn't sound normal, not for 2018. But anyway, thanks, I will qualify my column names as you suggest.
– peter.petrov
Nov 8 at 10:07
It is less exaggerated than what you originally had in your question. And I think it is true.
– Laurenz Albe
Nov 8 at 10:07
Your way of boldly stating that everything you don't understand is backwards gives me the impression that you don't have a lot of programming experience.
– Laurenz Albe
Nov 8 at 10:09
|
show 7 more comments
up vote
2
down vote
There is a name conflict between the variable id
that is defined by the RETURNS TABLE
clause and the column of the same name.
Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you rather than doing something that may not be what you intended.
Qualify the column reference like this to remove the ambiguity:
WHERE test.item.id = 44
Well, if I had a local (function level) variable, I would understand this error but not in this case... So you're saying that theid
inRETURNS TABLE(id int)
under the hood is just implemented as a variable? And that's why Postgres is saying that I have an ambiguity?
– peter.petrov
Nov 8 at 10:02
"Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you..." That's kind of exaggerated btw.
– peter.petrov
Nov 8 at 10:03
I don't thinkid
in the column list of the return table should clash with variables and column names of other tables. That doesn't sound normal, not for 2018. But anyway, thanks, I will qualify my column names as you suggest.
– peter.petrov
Nov 8 at 10:07
It is less exaggerated than what you originally had in your question. And I think it is true.
– Laurenz Albe
Nov 8 at 10:07
Your way of boldly stating that everything you don't understand is backwards gives me the impression that you don't have a lot of programming experience.
– Laurenz Albe
Nov 8 at 10:09
|
show 7 more comments
up vote
2
down vote
up vote
2
down vote
There is a name conflict between the variable id
that is defined by the RETURNS TABLE
clause and the column of the same name.
Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you rather than doing something that may not be what you intended.
Qualify the column reference like this to remove the ambiguity:
WHERE test.item.id = 44
There is a name conflict between the variable id
that is defined by the RETURNS TABLE
clause and the column of the same name.
Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you rather than doing something that may not be what you intended.
Qualify the column reference like this to remove the ambiguity:
WHERE test.item.id = 44
edited Nov 8 at 9:57
answered Nov 8 at 9:43
Laurenz Albe
40.9k92745
40.9k92745
Well, if I had a local (function level) variable, I would understand this error but not in this case... So you're saying that theid
inRETURNS TABLE(id int)
under the hood is just implemented as a variable? And that's why Postgres is saying that I have an ambiguity?
– peter.petrov
Nov 8 at 10:02
"Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you..." That's kind of exaggerated btw.
– peter.petrov
Nov 8 at 10:03
I don't thinkid
in the column list of the return table should clash with variables and column names of other tables. That doesn't sound normal, not for 2018. But anyway, thanks, I will qualify my column names as you suggest.
– peter.petrov
Nov 8 at 10:07
It is less exaggerated than what you originally had in your question. And I think it is true.
– Laurenz Albe
Nov 8 at 10:07
Your way of boldly stating that everything you don't understand is backwards gives me the impression that you don't have a lot of programming experience.
– Laurenz Albe
Nov 8 at 10:09
|
show 7 more comments
Well, if I had a local (function level) variable, I would understand this error but not in this case... So you're saying that theid
inRETURNS TABLE(id int)
under the hood is just implemented as a variable? And that's why Postgres is saying that I have an ambiguity?
– peter.petrov
Nov 8 at 10:02
"Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you..." That's kind of exaggerated btw.
– peter.petrov
Nov 8 at 10:03
I don't thinkid
in the column list of the return table should clash with variables and column names of other tables. That doesn't sound normal, not for 2018. But anyway, thanks, I will qualify my column names as you suggest.
– peter.petrov
Nov 8 at 10:07
It is less exaggerated than what you originally had in your question. And I think it is true.
– Laurenz Albe
Nov 8 at 10:07
Your way of boldly stating that everything you don't understand is backwards gives me the impression that you don't have a lot of programming experience.
– Laurenz Albe
Nov 8 at 10:09
Well, if I had a local (function level) variable, I would understand this error but not in this case... So you're saying that the
id
in RETURNS TABLE(id int)
under the hood is just implemented as a variable? And that's why Postgres is saying that I have an ambiguity?– peter.petrov
Nov 8 at 10:02
Well, if I had a local (function level) variable, I would understand this error but not in this case... So you're saying that the
id
in RETURNS TABLE(id int)
under the hood is just implemented as a variable? And that's why Postgres is saying that I have an ambiguity?– peter.petrov
Nov 8 at 10:02
"Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you..." That's kind of exaggerated btw.
– peter.petrov
Nov 8 at 10:03
"Things like that cause problems in all programming languages, only PostgreSQL is nice enough to alert you..." That's kind of exaggerated btw.
– peter.petrov
Nov 8 at 10:03
I don't think
id
in the column list of the return table should clash with variables and column names of other tables. That doesn't sound normal, not for 2018. But anyway, thanks, I will qualify my column names as you suggest.– peter.petrov
Nov 8 at 10:07
I don't think
id
in the column list of the return table should clash with variables and column names of other tables. That doesn't sound normal, not for 2018. But anyway, thanks, I will qualify my column names as you suggest.– peter.petrov
Nov 8 at 10:07
It is less exaggerated than what you originally had in your question. And I think it is true.
– Laurenz Albe
Nov 8 at 10:07
It is less exaggerated than what you originally had in your question. And I think it is true.
– Laurenz Albe
Nov 8 at 10:07
Your way of boldly stating that everything you don't understand is backwards gives me the impression that you don't have a lot of programming experience.
– Laurenz Albe
Nov 8 at 10:09
Your way of boldly stating that everything you don't understand is backwards gives me the impression that you don't have a lot of programming experience.
– Laurenz Albe
Nov 8 at 10:09
|
show 7 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204855%2fcolumn-reference-id-is-ambiguous-it-could-refer-to-either-a-pl-pgsql-variabl%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I guess you get the error, because you have a return value named id and a column in your table with the same name
– Jens
Nov 8 at 9:32
Well, OK... I don't quite have a return value
id
. I am returning a table (from this function) and that returned table has an id column. Also, I have a physical table (test.item
) which has anid
column. I am amazed why it is finding a clash between the two. Makes no sense to me. Note that I have read the full manual about PL/pgSQL, its syntax, etc. There is nothing there documenting this weird behavior.– peter.petrov
Nov 8 at 9:35