postgres中自定义类型的单个字段的唯一约束

Unique constraint on single field of a custom type in postgres

我的架构中有一个实体价格,它有一个属性 amount,属于 自定义 类型 money_with_currency

money_with_currency 基本上是类型(金额 Big Int,货币 char(3))。

价格实体属于产品。我想要做的是,在 product_id(foreign key) + currency 的组合上创建一个 unique constraint 。我该怎么做?

引用记录类型的单个字段有点棘手:

CREATE TYPE money_with_currency AS (amount bigint, currency char(3));

CREATE TABLE product_price
( 
  product_id integer              not null references product, 
  price      money_with_currency  not null
);

CREATE UNIQUE INDEX ON product_price(product_id, ((price).currency));