我有一個約會的表這種結構(分塊的可讀性):PostgreSQL的設置自定義約束
appointments:
- id
- staff_id
- start_time
- end_time
- cancelled
我想添加一個數據庫約束不能夠翻一番預訂約會。我在想,如果有可能的線沿線的添加約束條件:
when staff_id = ? and cancelled = false then set a "unique" constraint on start_time
如果這是不可能有類似的東西我可以做實現我的最終目標?
編輯
這是全預約表
CREATE TABLE "appointments" (
"id" uuid,
"customer_id" uuid NOT NULL REFERENCES customers ON DELETE CASCADE ON UPDATE CASCADE,
"staff_id" uuid NOT NULL REFERENCES staff ON DELETE CASCADE ON UPDATE CASCADE,
"start_time" timestamp NOT NULL,
"end_time" timestamp NOT NULL,
"notes" text,
"cancelled" boolean NOT NULL DEFAULT false,
"created_at" timestamp with time zone NOT NULL,
"updated_at" timestamp with time zone NOT NULL,
);
用排除法:
CREATE TABLE "appointments" (
"id" uuid,
"customer_id" uuid NOT NULL REFERENCES customers ON DELETE CASCADE ON UPDATE CASCADE,
"staff_id" uuid NOT NULL REFERENCES staff ON DELETE CASCADE ON UPDATE CASCADE,
"start_time" timestamp NOT NULL,
"end_time" timestamp NOT NULL,
"notes" text,
"cancelled" boolean NOT NULL DEFAULT false,
"created_at" timestamp with time zone NOT NULL,
"updated_at" timestamp with time zone NOT NULL,
EXCLUDE USING gist (
staff_id WITH =,
tsrange(start_time, end_time) WITH &&
) WHERE (NOT cancelled),
PRIMARY KEY ("id")
);
與排除錯誤執行:
data type uuid has no default operator class for access method "gist"
爲您所請求的選擇答案不工作。 [請參閱此答案,瞭解如何實際停止使用排除約束進行雙重預訂。](http://stackoverflow.com/a/43456174/124486) –