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

时间:2018-03-27 03:11:25

标签: postgresql unique-constraint

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

money_with_currency基本上是类型(金额大额,货币字符(3))。

价格实体属于产品。我想要做的是,在 product_id(外键)+货币的组合上创建唯一约束。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

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

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));