postgresql而不是插入触发器返回null的默认值。如何解决?

发布于 2025-01-27 22:47:40 字数 1274 浏览 4 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2025-02-03 22:47:40

您需要返回a

RETURN ROW('a5f1cbf5-a35a-4390-9ee3-536b607d40c2'::uuid, NEW.testname)

You need to return a ROW that matches the view structure so something like:

RETURN ROW('a5f1cbf5-a35a-4390-9ee3-536b607d40c2'::uuid, NEW.testname)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文