gpt4 book ai didi

sql - 如何在 ON CONFLICT 子句中消除 plpgsql 变量名的歧义?

转载 作者:行者123 更新时间:2023-11-29 11:42:32 24 4
gpt4 key购买 nike

给定这张表:

create table test (
name text primary key
);

我需要编写一个变量名与主键名冲突的 plpgsql 函数,我必须在 on conflict 子句中使用它:

create or replace function func(
name text -- this variable name...
) returns void language plpgsql as
$$
begin
insert into test (name) values (name)
on conflict (name) do update -- ...conflicts with this line
set name = func.name;
end;
$$;

这会编译,但会抛出一个不明确的列引用:

select * from func('one');
ERROR: column reference "name" is ambiguous
LINE 2: on conflict (name) do update
^
DETAIL: It could refer to either a PL/pgSQL variable or a table column.
QUERY: insert into test (name) values (name)
on conflict (name) do update
set name = func.name
CONTEXT: PL/pgSQL function func(text) line 3 at SQL statement

我尝试将完整的列名称指定为 on conflict (test.name) 不编译,或 ((test.name)) 编译:

create or replace function func(
name text
) returns void language plpgsql as
$$
begin
insert into test (name) values (name)
on conflict ((test.name)) do -- this fails too
update set name = func.name;
end;
$$;

但它也失败了:

select * from func('two');
ERROR: invalid reference to FROM-clause entry for table "test"
LINE 2: on conflict ((test.name)) do
^
HINT: There is an entry for table "test", but it cannot be referenced from this part of the query.
QUERY: insert into test (name) values (name)
on conflict ((test.name)) do
update set name = func.name
CONTEXT: PL/pgSQL function func(text) line 3 at SQL statement

有解决办法吗?


编辑:我找到了一个解决方法:

on conflict on constraint test_pkey do update

其中 test_pkey 是表名加上 _pkey。我不知道这有多可靠。我仍然想改为指定列名。

最佳答案

首先,name对变量和属性来说都是一个坏名字。当你同时拥有两者时,代码将不会好看。考虑到这一点,您可以使用带标签的 block “前缀”变量(在下面的示例中 <<fn>>``), and set variable_conflict` 以优先考虑列名,请参见下面的代码:

t=# create or replace function func(
name text
) returns void language plpgsql as
$$
#variable_conflict use_column
<<fn>>
declare name text :='blah';
begin
insert into test (name) values (name)
on conflict (name) do -- this no longer fails
update set name = fn.name;
end;
$$;
t=# insert into test select 'b';
INSERT 0 1
Time: 8.076 ms
t=# select func('b');
func
------

(1 row)

Time: 6.117 ms
t=# select * from test;
name
------
b
blah
(2 rows)

https://www.postgresql.org/docs/current/static/plpgsql-implementation.html#PLPGSQL-VAR-SUBST

By default, PL/pgSQL will report an error if a name in a SQL statement could refer to either a variable or a table column. You can fix such a problem by renaming the variable or column, or by qualifying the ambiguous reference, or by telling PL/pgSQL which interpretation to prefer.

而且 - 基本上整个链接都是关于它的。

然而 - 在演示了如何使用 plpgsql 轻松完成特定任务之后,我仍然引用 namual:

The simplest solution is to rename the variable or column. A common coding rule is to use a different naming convention for PL/pgSQL variables than you use for column names. For example, if you consistently name function variables v_something while none of your column names start with v_, no conflicts will occur.

关于sql - 如何在 ON CONFLICT 子句中消除 plpgsql 变量名的歧义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47678934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com