![](/img/trans.png)
[英]Postgres constraint name need to be unique across single table or entire schema?
[英]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.