【问题标题】:Row expansion via "*" is not supported here此处不支持通过“*”进行行扩展
【发布时间】:2013-08-22 22:52:13
【问题描述】:

我处于触发上下文中,并试图让追随 sn-p 工作。

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;

我收到以下错误

[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

这里不支持关于如何修复通过“*”进行行扩展的任何建议? 耦合到特定架构不是一种选择。

【问题讨论】:

  • 工作的非 hstore 执行 format('insert into %I select ''i'', now(), user, ($1).*', tg_table_name || '_audit') using new ;返回新的;

标签: postgresql triggers plpgsql dynamic-sql


【解决方案1】:

在我看来,它应该是这样工作的:

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);

最好使用USING 子句提供所有值。
你也可以cast a record to hstore directly with hstore(record)

【讨论】:

    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2023-03-08
    • 2022-12-16
    • 2012-01-22
    • 2022-11-07
    • 2016-05-17
    相关资源
    最近更新 更多