在 postgresql 中,执行 INSERT INTO RETURNING 时返回规则中函数的结果
我有一个包含多个字段的视图。
当我INSERT INTO
视图时,我运行一个基于INSERT
参数的函数。该函数返回一个值。
如何从规则中检索值?
INSERT RETURNING
给我:
错误:无法对关系“full_subntes”执行 INSERT RETURNING
提示:您需要一个带有 RETURNING 子句的无条件 ON INSERT DO INSTEAD 规则。
示例:
CREATE TABLE test (
a VARCAHR primary key,
b VARCHAR,
);
CREATE VIEW test_v AS SELECT * FROM test;
CREATE OR REPLACE RULE Test_v_Insert AS ON INSERT TO Test_v
DO INSTEAD (
SELECT myFunction('param');
);
INSERT INTO test_v(a, b) VALUES ('a', 'b') RETURNING a, b;
然后我收到上述错误。
I have a View that has several fields.
When i INSERT INTO
a view I run a function based on INSERT
parametrs. The function returns a value.
How can I retrieve The value from rule?
INSERT RETURNING
Gives me:
ERROR: cannot perform INSERT RETURNING on relation "full_subntes"
HINT: You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.
Example:
CREATE TABLE test (
a VARCAHR primary key,
b VARCHAR,
);
CREATE VIEW test_v AS SELECT * FROM test;
CREATE OR REPLACE RULE Test_v_Insert AS ON INSERT TO Test_v
DO INSTEAD (
SELECT myFunction('param');
);
INSERT INTO test_v(a, b) VALUES ('a', 'b') RETURNING a, b;
Then I get an error described above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个例子。
首先,我们创建一个测试表:
然后,我们创建一个视图:
接下来,创建更新规则:
最后,这是插入规则:
现在您可以插入一些测试数据:
并检查插入的元组:
Here is an example.
First, we create a test table:
Then, we create a view:
Next, the update rule is created:
And finally, here is the insert rule:
Now you can insert some test data:
and check the tuples inserted:
为了更新 Postgres 中的视图,您需要定义一个规则,告诉它更新基表。听起来您还没有创建规则。 http://www.postgresql.org/docs/current/interactive/规则更新.html
In order to update a view in Postgres, you need to define a rule telling it to update the base table instead. It doesn't sound like you've created a rule yet. http://www.postgresql.org/docs/current/interactive/rules-update.html