簡體   English   中英

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的組合上創建一個唯一約束 我怎樣才能做到這一點?

引用記錄類型的單個字段有點棘手:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM