如何正确地将数组从 Django/Python 传递到 Postgres/PLPGSQL 存储过程可变参数
how to correctly pass arrays from Django/Python to Postgres/PLPGSQL stored proc variadic parameter
我正在尝试从 Django/Python 调用 Postgres/PLPGSQL 上的存储过程。我使用 VARIADIC 参数定义了存储过程:
CREATE OR REPLACE FUNCTION udf_getmultiplecategoriescodetypes (VARIADIC NUMERIC[])
然后我想在 proc 中使用参数数组的唯一地方是在 WHERE stmt:
WHERE cct.code_category_fk_id = ANY()
当我从 DBeaver 控制台调用该函数时,所有这些都完美运行:
SELECT * FROM udf_getmultiplecategoriescodetypes(1, 2)
但是,如果我在 Django/Python 中使用 callproc 函数,使用相同类型的语法,如下所示:
c.callproc("udf_getmultpilecategoriescodetypes", (1, 2))
我收到错误:
LINE 1: SELECT * FROM udf_getmultpilecategoriescodetypes(1,2)
HINT: No function matches the given name and argument types. You might need to add
explicit type casts.
function udf_getmultpilecategoriescodetypes(integer, integer) does not exist
此外,在 DBeaver 中,当函数被创建并存储在函数列表中时,如果我尝试删除它,它说找不到该函数。
Function Showing in TreeView
Deletion Error Msg
后来我发现我可以通过使用 DROP FUNCTION 并包含 VARIADIC 参数来删除它,这样它就可以根据参数的数量和类型来识别它。但是为什么会这样呢?
那么,两个问题:
- 将整数数组从 Django/Python callproc 函数传递到 Postgres/PLPGSQL 存储过程中的 VARIADIC 参数的正确方法是什么?
- 当使用数组或 VARIADIC 作为参数时,为什么 DBeaver 无法将列出的存储过程函数识别为存在?这可能与 callproc 错误有关,因为这两个问题的错误似乎都与 VARIADIC 参数有关?
- What is the correct way to pass an array of integers from a Django/Python callproc function to a VARIADIC parameter in a Postgres/PGPLSQL stored proc?
您将参数定义为 VARIADIC NUMERIC[]
,因此您确实想传递 numeric
的 数组,而不是 数组整数.
并且由于它是一个 VARIADIC
函数,您可以传递 numeric
值的 列表 而不是实际的数组 - 就像您所做的那样。参见:
- Pass multiple values in single parameter
但这不是手头的问题。如果没有 func(int[])
,Postgres function type resolution 将退回到 func(numeric[])
。似乎是一个普通的打字错误。你看出区别了吗?
udf_getmultiplecategoriescodetypes
<strike>udf_getmultpilecategoriescodetypes</strike>
更简洁的名称和一些下划线可能有助于防止此类拼写错误。
- Why does DBeaver not recognise a listed stored proc function as existing when an array or VARIADIC is used as the parameter? And might this somehow be related to the callproc error, since the errors of both issues seem to be related to the VARIADIC parameter?
Postgres 允许函数重载。因此,函数签名由其名称 和 参数组成,以确保明确。 DBeaver 与它无关。
最重要的是,请注意同一功能可以存在于多个模式中。因此,请确保您使用正确的 search_path
操作并且没有(并且无意中调用)另一个模式中的副本。
因此,由于缺少参数,您尝试删除函数 public.udf_getmultiplecategoriescodetypes()
失败了。如果函数是在不同的架构中创建的,它可能也会失败。
相关:
- Error: No function matches the given name and argument types
- How does the search_path influence identifier resolution and the "current schema"
我正在尝试从 Django/Python 调用 Postgres/PLPGSQL 上的存储过程。我使用 VARIADIC 参数定义了存储过程:
CREATE OR REPLACE FUNCTION udf_getmultiplecategoriescodetypes (VARIADIC NUMERIC[])
然后我想在 proc 中使用参数数组的唯一地方是在 WHERE stmt:
WHERE cct.code_category_fk_id = ANY()
当我从 DBeaver 控制台调用该函数时,所有这些都完美运行:
SELECT * FROM udf_getmultiplecategoriescodetypes(1, 2)
但是,如果我在 Django/Python 中使用 callproc 函数,使用相同类型的语法,如下所示:
c.callproc("udf_getmultpilecategoriescodetypes", (1, 2))
我收到错误:
LINE 1: SELECT * FROM udf_getmultpilecategoriescodetypes(1,2)
HINT: No function matches the given name and argument types. You might need to add
explicit type casts.
function udf_getmultpilecategoriescodetypes(integer, integer) does not exist
此外,在 DBeaver 中,当函数被创建并存储在函数列表中时,如果我尝试删除它,它说找不到该函数。
Function Showing in TreeView
Deletion Error Msg
后来我发现我可以通过使用 DROP FUNCTION 并包含 VARIADIC 参数来删除它,这样它就可以根据参数的数量和类型来识别它。但是为什么会这样呢?
那么,两个问题:
- 将整数数组从 Django/Python callproc 函数传递到 Postgres/PLPGSQL 存储过程中的 VARIADIC 参数的正确方法是什么?
- 当使用数组或 VARIADIC 作为参数时,为什么 DBeaver 无法将列出的存储过程函数识别为存在?这可能与 callproc 错误有关,因为这两个问题的错误似乎都与 VARIADIC 参数有关?
- What is the correct way to pass an array of integers from a Django/Python callproc function to a VARIADIC parameter in a Postgres/PGPLSQL stored proc?
您将参数定义为 VARIADIC NUMERIC[]
,因此您确实想传递 numeric
的 数组,而不是 数组整数.
并且由于它是一个 VARIADIC
函数,您可以传递 numeric
值的 列表 而不是实际的数组 - 就像您所做的那样。参见:
- Pass multiple values in single parameter
但这不是手头的问题。如果没有 func(int[])
,Postgres function type resolution 将退回到 func(numeric[])
。似乎是一个普通的打字错误。你看出区别了吗?
udf_getmultiplecategoriescodetypes
<strike>udf_getmultpilecategoriescodetypes</strike>
更简洁的名称和一些下划线可能有助于防止此类拼写错误。
- Why does DBeaver not recognise a listed stored proc function as existing when an array or VARIADIC is used as the parameter? And might this somehow be related to the callproc error, since the errors of both issues seem to be related to the VARIADIC parameter?
Postgres 允许函数重载。因此,函数签名由其名称 和 参数组成,以确保明确。 DBeaver 与它无关。
最重要的是,请注意同一功能可以存在于多个模式中。因此,请确保您使用正确的 search_path
操作并且没有(并且无意中调用)另一个模式中的副本。
因此,由于缺少参数,您尝试删除函数 public.udf_getmultiplecategoriescodetypes()
失败了。如果函数是在不同的架构中创建的,它可能也会失败。
相关:
- Error: No function matches the given name and argument types
- How does the search_path influence identifier resolution and the "current schema"