如何将数组从Django/Python正确传递到Postgres/PLPGSQL存储的proc variadic param

2024-10-03 06:25:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从Django/Python调用Postgres/PLPGSQL上的存储过程。我使用可变参数定义了存储过程:

CREATE OR REPLACE FUNCTION udf_getmultiplecategoriescodetypes (VARIADIC NUMERIC[])

那么我只想在proc中使用参数数组的地方是WHERE stmt:

^{pr2}$

当我从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

如果在函数中找不到它,那么当函数在dber中被创建时,我会说它不能被保存在函数中。在

Function Showing in TreeView

Deletion Error Msg

我发现我可以使用DROP函数删除它,并包含可变参数,这样它就可以根据参数的数量和类型来识别它。但为什么会这样?在

所以,有两个问题:

  1. 将整数数组从Django/Python callproc函数传递到Postgres/PLPGSQL存储过程中的可变参数的正确方法是什么?
  2. 当使用数组或变量作为参数时,为什么DBeaver不将列出的存储过程函数识别为存在的?这是否与callproc错误有关,因为这两个问题的错误似乎都与可变参数有关?

Tags: django函数from参数过程错误postgres数组
1条回答
网友
1楼 · 发布于 2024-10-03 06:25:50
  1. 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数组,而不是integer上的数组。在

因为它是一个VARIADIC函数,所以可以传递一个numeric值的列表,而不是像您一样传递一个实际的数组。参见:

但这不是眼下的问题。如果没有func(int[]),Postgresfunction type resolution将返回到func(numeric[])。似乎是一个简单的打字错误。你看到区别了吗?在

udf_getmultiplecategoriescodetypes
udf_getmultpilecategoriescodetypes

简明扼要的名字和一些下划线可能有助于防止此类打字错误。在

  1. 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()失败。如果函数是在不同的模式下创建的,它也可能失败。在

相关:

相关问题 更多 >