如何在 EXCLUDE 约束中将 uuid 与 postgresql 要点一起使用
How to use uuid with postgresql gist in EXCLUDE constraint
我在使用要点排除约束时遇到错误
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
数据类型是句点
我正在使用 PostgreSQL 9.4。我只能使用 9.4,因为我没有任何其他选择,因为我无法在 9.5、9.6 和 10 中安装 temporal
扩展程序。
为此您需要 btree_gist
扩展程序:
btree_gist
provides GiST index operator classes that implement B-tree equivalent behavior for the data types int2
, int4
, int8
, float4
, float8
, numeric
, timestamp with time zone
, timestamp without time zone
, time with time zone
, time without time zone
, date
, interval
, oid
, money
, char
, varchar
, text
, bytea
, bit
, varbit
, macaddr
, macaddr8
, inet
, cidr
, uuid
, and all enum
types.
遗憾的是,仅在 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 &&)|)
我在使用要点排除约束时遇到错误
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
数据类型是句点
我正在使用 PostgreSQL 9.4。我只能使用 9.4,因为我没有任何其他选择,因为我无法在 9.5、9.6 和 10 中安装 temporal
扩展程序。
为此您需要 btree_gist
扩展程序:
btree_gist
provides GiST index operator classes that implement B-tree equivalent behavior for the data typesint2
,int4
,int8
,float4
,float8
,numeric
,timestamp with time zone
,timestamp without time zone
,time with time zone
,time without time zone
,date
,interval
,oid
,money
,char
,varchar
,text
,bytea
,bit
,varbit
,macaddr
,macaddr8
,inet
,cidr
,uuid
, and allenum
types.
遗憾的是,仅在 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 &&)|)