Re: Creating constraint dynamically

Поиск
Список
Период
Сортировка
От sivapostgres@yahoo.com
Тема Re: Creating constraint dynamically
Дата
Msg-id 129838278.823301.1661153341365@mail.yahoo.com
обсуждение исходный текст
Ответы Re: Creating constraint dynamically  (Wim Bertels <wim.bertels@ucll.be>)
Re: Creating constraint dynamically  (Francisco Olarte <folarte@peoplecall.com>)
Re: Creating constraint dynamically  (jian he <jian.universality@gmail.com>)
Список pgsql-general
Hello,
Using PG 11.4

We populate constraint string dynamically and add it to the table with alter table command.  It gets added, but without the required brackets.  
What we build is
ALTER TABLE public.tx_barcode_stock 
ADD CONSTRAINT "tx_barcode_stock_CK1" CHECK 
( (branchcode = '1'::bpchar  and barcodeitem = 'Y'::bpchar and closingstock >= 0::numeric)  Or (branchcode = '1' and barcodeitem = 'N'::bpchar and closingstock >= 0::numeric )  Or (branchcode = '2'::bpchar  and barcodeitem = 'Y'::bpchar and closingstock >= 0::numeric)  Or (branchcode = '2' and  barcodeitem = 'N'::bpchar and closingstock >= 0::numeric ) ) NOT VALID; 

After creation, when we check what we find is  [ in PgAdmin ]
ALTER TABLE public.tx_barcode_stock
    ADD CONSTRAINT "tx_barcode_stock_CK1" CHECK (branchcode::bpchar = '1'::bpchar AND barcodeitem = 'Y'::bpchar AND closingstock >= 0::numeric OR branchcode::text = '1'::text AND barcodeitem = 'N'::bpchar AND closingstock >= 0::numeric OR branchcode::bpchar = '2'::bpchar AND barcodeitem = 'Y'::bpchar AND closingstock >= 0::numeric OR branchcode::text = '2'::text AND barcodeitem = 'N'::bpchar AND closingstock >= 0::numeric)
    NOT VALID;

We have only one bracket, in the final updated one.

Since there are AND and OR conditions, without brackets the whole conditions becomes useless.  

How to create a constraint like the above one, with braces in tact ?   Or any other way that we can implement a check constraint as above?

Happiness Always
BKR Sivaprakash

В списке pgsql-general по дате отправления:

Предыдущее
От: Wim Bertels
Дата:
Сообщение: Re: Can I get the number of results plus the results with a single query?
Следующее
От: Wim Bertels
Дата:
Сообщение: Re: Creating constraint dynamically