Row expansion via “*” is not supported here

依然范特西╮ 提交于 2019-12-11 09:26:05

问题


I'm in a trigger context and trying to get following snippet to work.

execute format('insert into %I (user_name, action, new_values, query) 
    values (''%I'', ''i'', hstore(($1).*), current_query())', 
     tg_table_name::text || '_audit', current_user::text)
using new;

I'm getting the following error

[SQL]insert into book (title, n_pages) values ('PG is Great', 250);

[Err] ERROR:  row expansion via "*" is not supported here
LINE 2: values ('u1', 'i', hstore(($1).*), current_q...
                                               ^
QUERY:  insert into book_audit (user_name, action, new_values, query) 
        values ('u1', 'i', hstore(($1).*), current_query())
CONTEXT:  PL/pgSQL function "if_modified_func" line 8 at EXECUTE statement

Any suggestion on how to fix row expansion via "*" is not supported here ? Coupling to specific schema is not an option.


回答1:


From the top of my head, it should work like this:

EXECUTE format('
   INSERT INTO %I (user_name, action, new_values, query) 
   SELECT $1, ''i'', $2, current_query()'
   , tg_table_name::text || '_audit')
USING current_user, hstore(NEW);

It is better to supply all values with the USING clause.
And you can cast a record to hstore directly with hstore(record).



来源:https://stackoverflow.com/questions/18342652/row-expansion-via-is-not-supported-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!