[英]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 索引运算符类,它们为数据类型int2
、int4
、int8
、float4
、float8
、numeric
、timestamp with time zone
、timestamp without time zone
、time with time zone
、time without time zone
、date
实现 B 树等效行为、interval
、oid
、money
、char
、varchar
、text
、bytea
、bit
、varbit
、macaddr
、macaddr8
、inet
、cidr
、uuid
和所有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.