我在从 postgres 函数中进行选择时遇到问题,该函数返回 setof 用户定义的复合类型。我在这里创建了一个 MRE 项目: https: //github.com/sify21/testjooq
我的模式
create table person (
id bigint primary key generated by default as identity,
name varchar(50) not null,
age int not null,
unique(name)
);
create type dressing as (
id bigint,
name varchar(50),
age int,
costume varchar(50)
);
CREATE OR REPLACE FUNCTION merge_person(arr dressing[]) RETURNS SETOF dressing LANGUAGE plpgsql AS
$$
DECLARE x dressing;
BEGIN
FOREACH x IN ARRAY arr LOOP
return query insert into person(name, age) values (x.name, x.age) on conflict (name) do update set age=x.age returning id,name,age,x.costume;
END LOOP;
RETURN;
END;
$$;
首先,我不能使用create.selectFrom(Routines.mergePerson(records))
,它报告这个错误
org.jooq.exception.DataAccessException: SQL [select "merge_person"."merge_person" from "public"."merge_person"(cast(? as "public"."dressing"[]))]; ERROR: column merge_person.merge_person does not exist
我不知道它select "merge_person"."merge_person"
从哪里来的,显然merge_person
返回了dressing
记录。我检查了生成的代码,方法com.test.db.tables.records.MergePersonRecord
上有一个警告setMergePerson(Object value)
,说它是Unknown data type
,但我不知道如何修复它。
其次,如果我使用create.select(DSL.asterisk()).from(Routines.mergePerson(records))
,记录保存在postgres中,但返回的结果只包含id
字段,这是输出
+------------+
|merge_person|
+------------+
|1 |
|2 |
+------------+
但这不是我想要的,我希望它能DressingRecord
回来