我在Spring 4应用程序中使用jOOQ 3.8.4和PostgreSQL 9.5。我有以下表格和类型定义
CREATE DOMAIN shop.money_amount AS numeric(6,2) DEFAULT 0 NOT NULL CHECK (value > 0::numeric);
CREATE TYPE shop.money AS (
m_amount shop.money_amount,
m_currency shop.currency,
m_country shop.site_country
);
CREATE TYPE shop.money_mapping AS (
mm_moneys shop.money []
);
CREATE TABLE shop.article
(
a_id bigserial NOT NULL,
a_price shop.money_mapping NOT NULL,
CONSTRAINT a_pk_id PRIMARY KEY (a_id)
);
然后我尝试使用jOOQ进行插入,即:
MoneyMappingRecord priceMoneyMapping = new MoneyMappingRecord();
priceMoneyMapping.setMoneys(new MoneyRecord[]{
new MoneyRecord().setAmount(new BigDecimal("11")).setCountry(SiteCountry.US).setCurrency(Currency.USD),
new MoneyRecord().setAmount(new BigDecimal("14")).setCountry(SiteCountry.DE).setCurrency(Currency.EUR)
});
dsl.insertInto(ARTICLE)
.set(ARTICLE.A_PRICE, priceMoneyMapping)
.returning(ARTICLE.A_ID).fetchOne().getId();
然后我得到:
Caused by: org.springframework.jdbc.BadSqlGrammarException: jOOQ;
bad SQL grammar [insert into "shop"."article" ("a_price") values
(row(?::money[])) returning "shop"."article"."a_id"]; nested
exception is org.postgresql.util.PSQLException:
ERROR: cannot cast type record to shop.money_mapping
Detail: Cannot cast type money[] to shop.money[] in column 1.
我做错了什么?
更新
我尝试按照Lukas的建议重命名shop.money类型(从shop.money
到shop.localized_money
),但我相信问题与架构有关。请参阅更新的错误。
Caused by: org.springframework.jdbc.BadSqlGrammarException:
jOOQ; bad SQL grammar [insert into "shop"."article" ("a_price")
values (row(?::localized_money[])) returning "shop"."article"."a_id"]; nested exception is
org.postgresql.util.PSQLException: ERROR: type "localized_money[]" does not exist
类型中的类型可能是个问题!
答案 0 :(得分:1)
在jOOQ 3.8中,似乎存在与嵌套数组类型相关的问题。我为此创建了一个问题:https://github.com/jOOQ/jOOQ/issues/5571
问题是当绑定为绑定变量时,您的自定义类型数组类型需要完全限定。如果它不是完全限定的,那么PostgreSQL找不到该类型。此问题中描述了一种可能的解决方法:Permanently Set Postgresql Schema Path
您可以在用户的搜索路径中添加shop
架构:
ALTER ROLE <your_login_role> SET search_path TO shop;
...这意味着shop
架构内的元素不再需要完全限定。这也可能是件坏事,所以要小心! :)