postgresql Supabase Postgres API触发的函数为子查询返回NULL结果

rvpgvaaj  于 2023-03-12  发布在  PostgreSQL
关注(0)|答案(1)|浏览(153)

我已经在表上创建了一个触发器。当触发器被激活时,触发器将调用一个进程从Salesforce中拉下JSON数据。Salesforce定期使其API密钥过期,我必须在调出期间拉入一个新的API密钥。
我遇到的问题是,当触发器触发并将所需信息传递给调用sales force的函数时,从另一个表/进程获取更新的API键的查询结果为NULL,调用失败。
我不知道如何正确调试这个问题以获得更多信息,也不知道如何修复它。奇怪的是,当我在控制台单独运行进程时,它工作并拉动键,但当触发器处理它时,获取API密钥的查询结果显示为NULL。2可能是事务处理有一些限制,或者需要异步运行。任何帮助将不胜感激!下面是代码:
在Supabase上运行的POSTGRES V 15.1

*****************TRIGGER*****************************

CREATE TRIGGER api_request_sf_trigger
    AFTER INSERT
    ON public.api_request
    FOR EACH ROW
    EXECUTE FUNCTION public.api_request_sf_trigger_func();

*****************TRIGGER FUNCTION********************

CREATE OR REPLACE FUNCTION public.api_request_sf_trigger_func()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
  
Declare
  new_type text=new.type;
  new_id text=new.foreign_id;
  sf_version text='57.0';
  
BEGIN
  RAISE LOG    'Initial trigger new.type is %', new.type;
  RAISE LOG    'Initial triggernew.foreign_id is %', new.foreign_id;
  EXECUTE public.get_sf_api((select format('%s',sf_version)), (select format('%s',new_type)) ,(select format('%s',new_id)));
Return New;

END;
$BODY$;

***********************FUNCTION CALLED BY TRIGGER******* BELOW**************

CREATE OR REPLACE FUNCTION public.get_sf_api(
sf_version text,
sobject text,
sf_foreignid text)
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$

declare 
sf_record jsonb;  
sf_lastupdate timestamp;
url text;
vapi_key text;
key_header text;

begin

--ERROR AREA:  CODE TO GETS API KEY WORKS WHEN THIS FUNCTION IS EXECUTED SEPRARETLY IN PGADMIN     BUT RETURNS NULL WHEN RUN FROM TRIGGER

--OPT1 HARD CODE API KEY WORKS WITH TRIGGER
--vapi_key='HARD CODED API KEY';

--OPT2 WORKS IN CONSOLE BUT NOT IN TRIGGER BECAUSE IT RETURNS NULL(get key from other process)
--vapi_key=(Select public.get_sf_token());--refreshes API key in a separate process.

--OPT3 WORKS IN CONSOLE BUT NOT IN TRIGGER BECAUSE IT RETURNS NULL (get key from config table)
--vapi_key=(select api_key from api_info where system='Salesforce' and type='access_token' limit     1);

--ERROR AREA ABOVE********************************************************************************

RAISE LOG 'Passed sf version is %', sf_version;
RAISE LOG 'Passed sobject is %', sobject;
RAISE LOG 'Passed sf foreign_id is %', sf_foreignid;
RAISE LOG 'API KEY is %', vapi_key;

key_header=concat('OAuth ',trim(both '"' from vapi_key));

RAISE LOG 'API Key Header is %', key_header;

url=(select Concat('https://PRIVATEURL.my.salesforce.com/services/data/v',sf_version,'/query/?  q=Select+Fields(ALL)+From+',sobject,'+Where+Id=''',(trim(both '"' from sf_foreignId)),'''+Limit+1')); 

RAISE LOG '%', url;

SELECT CONTENT::jsonb->'records'->0 as records FROM http((
      'GET',
       url,
       ARRAY[http_header('Authorization',key_header)],         
       NULL,   
       NULL
    )::http_request)

INTO sf_record;  

RAISE LOG 'Passed value from other function sf_record is %', sf_record;  
sf_lastupdate=sf_record::jsonb->'LastModifiedDate'; 
RAISE LOG 'Passed value from other function sf_last updated date is %', sf_lastupdate;  

Insert INTO api_data(json_body,foreign_id,object_type,foreign_updated)
                Values (sf_record::jsonb,sf_foreignid,sobject,sf_lastupdate);

end;
$BODY$;

第四次编辑********

CREATE OR REPLACE FUNCTION public.api_request_sf_trigger_func()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
  
Declare

vapi_key text;

BEGIN

vapi_key=(select api_key from api_info limit 1);

RAISE LOG '3rd Trigger vapi_key is %',vapi_key;

Return New;

END;
$BODY$;

当通过Supabase API运行时,简单的select语句在上面的SIMPLE第4个触发器中不起作用。当我在控制台中进行插入时,它起作用并在select语句中提取API键,但当我通过API运行它时,它不起作用。

3zwtqj6y

3zwtqj6y1#

这最终只是一个API数据表的权限问题,这并不明显,但是如果您要返回NULL值,请检查Supabase权限。

相关问题