Postgres不区分大小写的排除约束

时间:2016-08-17 19:29:17

标签: postgresql database-design constraints

我正在尝试创建一个排除约束,以防止重叠的有效时间戳范围,但只比较具有相同文本值的记录的范围。我希望文本值比较不区分大小写。我可以将=运算符与文本字段一起使用,但不能与citext字段一起使用,~~*运算符不可交换。

这是PostgreSQL 9.5,并且在数据库上创建了citextbtree_gist扩展。

CREATE TABLE customer_product_categories (
  id serial PRIMARY KEY, 
  name text NOT NULL, 
  effective tstzrange DEFAULT '[-infinity,infinity]', 
  EXCLUDE USING gist (name WITH ~~*, effective WITH &&)
)
ERROR: operator ~~*(text,text) is not commutative
CREATE TABLE customer_product_categories (
  id serial PRIMARY KEY, 
  name citext NOT NULL, 
  effective tstzrange DEFAULT '[-infinity,infinity]', 
  EXCLUDE USING gist (name WITH =, effective WITH &&)
)
ERROR: operator =(citext,citext) is not a member of operator family "gist_text_ops"
  • ILIKE如何不可交换?
  • 有没有办法做我想做的事,或者我是傻瓜的差事?

1 个答案:

答案 0 :(得分:2)

A-公顷!我想到了。我不得不去上学:

CREATE TABLE "customer_product_categories" (
  "id" serial PRIMARY KEY, 
  "name" text NOT NULL, 
  "effective" tstzrange DEFAULT '[-infinity,infinity]', 
  EXCLUDE USING gist (LOWER("name") WITH =, "effective" WITH &&)
)