Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

postgresql - case insensitive uniqueness - how to update against the current record, regardless of case?

I am using Hasura on top of Postgres to store companies and other information. The source data has the same spelling of records, but in differing cases - for example:

name country
Reckitt Benckiser (Aust) Pty Ltd US
RECKITT BENCKISER (AUST) PTY LTD AU
question from:https://stackoverflow.com/questions/65660279/case-insensitive-uniqueness-how-to-update-against-the-current-record-regardle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You didn't specify what version of Postgres you are running, but if it is 12 or above you can add a virtual (generated) column, then put a unique constraint on that column. See example:

alter table table_name add v_company_name text generated always as ( lower(company_name) stored);
alter table table_name add constraint company_name_bk unique (v_company_name);  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...