gpt4 book ai didi

sql - 列重命名后函数调用失败(错误 42703 "column does not exist")

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

简单场景:

CREATE TABLE foo (bar int);

INSERT INTO foo VALUES (42);

CREATE FUNCTION baz() RETURNS int AS $$
SELECT bar
FROM foo
$$ LANGUAGE SQL;

SELECT baz();

这有效并返回 42。

我不喜欢我的专栏命名,所以我重命名了它:

ALTER TABLE foo RENAME bar TO qux;

但是现在如果我再次调用该函数:

SELECT baz();

[42703] ERROR: column "bar" does not exist

SQL function "baz" during inlining

我希望将重命名“级联”到函数体,因为 PostgreSQL 会阻止创建具有无效列引用的函数。

我真的必须重新创建每个以旧名称引用该列的函数吗?使用版本 10.7。

最佳答案

文档说函数体是a string constant whose meaning depends on the language .这由语言处理程序在运行时解释,并且 the database server has no built-in knowledge about how to interpret the function's source text .

因此 PostgreSQL 不跟踪任何依赖关系,并且该函数按名称而不是按对象 ID 引用表和列(就像 View 一样)。

因此 PostgreSQL 不能仅仅因为其中引用的表列以 PostgreSQL 未知的语法更改其名称而自动更改函数体字符串。

关于sql - 列重命名后函数调用失败(错误 42703 "column does not exist"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58942235/

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