postgresql而不是插入触发器返回null的默认值。如何解决?
我正在使用Postgres 13版本,并在上面看到奇怪的行为而不是插入触发器。 在插入触发器的输出中,默认值为null。但是该值在列中提供,我应该如何更改从触发器的新返回以具有该值?
CREATE TABLE test1
(test_id UUID NOT NULL DEFAULT uuid_generate_v4(),
testname VARCHAR(128));
CREATE TABLE test2
(test_id UUID NOT NULL DEFAULT uuid_generate_v4(),
testname VARCHAR(128));
CREATE VIEW test_view
AS
SELECT *
FROM test1
UNION ALL
SELECT *
FROM test2;
CREATE OR REPLACE FUNCTION public.insert_test()
RETURNS trigger
SECURITY DEFINER
AS
$func$
BEGIN
--some functionality understanding where to insert records
IF 1 = 1 THEN
INSERT INTO public.test1
(
testname
)
VALUES
(NEW.testname
);
ELSE
INSERT INTO public.test2
(
testname
)
VALUES
(NEW.testname
);
END IF;
RETURN NEW;
END
$func$ LANGUAGE plpgsql;
CREATE TRIGGER TRG_test_migration_insert
INSTEAD OF INSERT ON public.test_view
FOR EACH ROW EXECUTE PROCEDURE public.insert_test();
INSERT INTO test_view
(testname)
VALUES
('abc')
RETURNING *;
INSERT INTO test1
(testname)
VALUES
('def')
RETURNING *;
因此,返回将是不同的,我如何修改触发器以在返回中具有默认值? 我试图拥有一些东西
RETURN SELECT v_test_id, new.testname
,但它不起作用。 谢谢你!
I am using Postgres 13 version and seeing strange behaviour on Instead of Insert trigger.
In the output of Insert trigger Default values are NULL. But the value is provided in column, how should I change return NEW from trigger to have that value?
CREATE TABLE test1
(test_id UUID NOT NULL DEFAULT uuid_generate_v4(),
testname VARCHAR(128));
CREATE TABLE test2
(test_id UUID NOT NULL DEFAULT uuid_generate_v4(),
testname VARCHAR(128));
CREATE VIEW test_view
AS
SELECT *
FROM test1
UNION ALL
SELECT *
FROM test2;
CREATE OR REPLACE FUNCTION public.insert_test()
RETURNS trigger
SECURITY DEFINER
AS
$func$
BEGIN
--some functionality understanding where to insert records
IF 1 = 1 THEN
INSERT INTO public.test1
(
testname
)
VALUES
(NEW.testname
);
ELSE
INSERT INTO public.test2
(
testname
)
VALUES
(NEW.testname
);
END IF;
RETURN NEW;
END
$func$ LANGUAGE plpgsql;
CREATE TRIGGER TRG_test_migration_insert
INSTEAD OF INSERT ON public.test_view
FOR EACH ROW EXECUTE PROCEDURE public.insert_test();
INSERT INTO test_view
(testname)
VALUES
('abc')
RETURNING *;
INSERT INTO test1
(testname)
VALUES
('def')
RETURNING *;
So the returning will be different, how I could modify the trigger to have default value in the returning?
I've tried to have something
RETURN SELECT v_test_id, new.testname
but it did not work.
THank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要返回a
You need to return a ROW that matches the view structure so something like: