pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: sivapostgres@yahoo.com <sivapostgres@yahoo.com>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Creating constraint dynamically
Date: Mon, 5 Sep 2022 06:02:51 +0000 (UTC)
Message-ID: <178956102.724667.1662357771396@mail.yahoo.com> (raw)
In-Reply-To: <CACJufxEn9V2bLVUk1z5H5sFJnBBc0cA8HzFFVx3VcNjhyAKqcQ@mail.gmail.com>
References: <129838278.823301.1661153341365.ref@mail.yahoo.com>
	<129838278.823301.1661153341365@mail.yahoo.com>
	<CACJufxEn9V2bLVUk1z5H5sFJnBBc0cA8HzFFVx3VcNjhyAKqcQ@mail.gmail.com>

 Hello,
Sorry for delay... I had to go on leave..
Checked, it's our mistake here.   The issue is not what I described, but in the replicated database where the constraint was not updated.  Correcting the constraint there solved this issue.  
Sorry for not checking properly before coming here.
Happiness Always
BKR Sivaprakash

    On Monday, 22 August, 2022 at 03:54:30 pm IST, jian he <jian.universality@gmail.com> wrote:  
 
 

On Mon, Aug 22, 2022 at 12:59 PM sivapostgres@yahoo.com <sivapostgres@yahoo.com> wrote:

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 isALTER 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


I guess it's because  pgadmin uses pg_get_constraintdef ( constraint oid [, pretty boolean ] ) pretty is true.if you use pg_get_constraintdef ( constraint oid ,false )  you will see more braces.
 
-- 
 I recommend David Deutsch's <<The Beginning of Infinity>>

  Jian


  

view thread (6+ messages)

Message-ID: <178956102.724667.1662357771396@mail.yahoo.com>
Permalink:  ../178956102.724667.1662357771396@mail.yahoo.com/
Also on:    postgresql.org/message-id/178956102.724667.1662357771396@mail.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: sivapostgres@yahoo.com
  Subject: Re: Creating constraint dynamically
  In-Reply-To: <178956102.724667.1662357771396@mail.yahoo.com>
  References: <129838278.823301.1661153341365.ref@mail.yahoo.com> <129838278.823301.1661153341365@mail.yahoo.com> <CACJufxEn9V2bLVUk1z5H5sFJnBBc0cA8HzFFVx3VcNjhyAKqcQ@mail.gmail.com> <178956102.724667.1662357771396@mail.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox