繁体   English   中英

如何在排除约束中将 uuid 与 postgresql gist 一起使用

[英]How to use uuid with postgresql gist in EXCLUDE constraint

使用 gist 使用排除约束时出现错误

ALTER TABLE tbl_product ADD EXCLUDE USING gist (base_id WITH =, lifetime WITH &&);

ERROR:  data type uuid has no default operator class for access method "gist"
HINT:  You must specify an operator class for the index or define a default operator class for the data type.

注意: base_id数据类型是 uuid, lifetime数据类型是 period

我正在使用 PostgreSQL 9.4。 我必须只使用 9.4,因为我没有任何其他选择,因为我无法在 9.5、9.6 和 10 中安装temporal扩展会出现错误。

你需要btree_gist扩展:

btree_gist提供了 GiST 索引运算符类,它们为数据类型int2int4int8float4float8numerictimestamp with time zonetimestamp without time zonetime with time zonetime without time zonedate实现 B 树等效行为、 intervaloidmoneycharvarchartextbyteabitvarbitmacaddrmacaddr8inetcidruuid和所有enum类型。

不幸的是,仅在 v10 中添加了对uuid支持。

使用 v10,您应该可以使用

base_id gist_uuid_ops WITH =

在您的排除约束中。

使用 9.4,您可以先将列转换为不同的类型:

(base_id::text) gist_text_ops WITH =

接受的答案是正确的,需要btree_gist ,但是建议的解决方案不起作用(至少在 2021 年的 v12 中不起作用)。 如果您在原始问题中遇到错误,您应该执行以下操作:

CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE tbl_product ADD EXCLUDE USING gist (base_id WITH =, lifetime WITH &&);

作为奖励,我一直在 Elixir & Ecto 3.5 中使用它,这里是如何在 Ecto 迁移中执行此操作:

execute "CREATE EXTENSION IF NOT EXISTS btree_gist"
create constraint(:tbl_product, "constraint_name", exclude: ~s|gist ("base_id" WITH =, lifetime WITH &&)|)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM