我有一个约会的表这种结构(分块的可读性):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) –