Re: Dropping foreign key only if it exists

From: Lukasz Brodziak <lukasz(dot)brodziak(at)gmail(dot)com>
To: Viktor Bojović <viktor(dot)bojovic(at)gmail(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: Dropping foreign key only if it exists
Date: 2011-04-20 11:11:13
Message-ID: BANLkTi=c5fhYiMJ6omdu=6j6_Umnqf_DGg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

The function:
CREATE OR REPLACE FUNCTION dropfk()
RETURNS integer AS
$BODY$
DECLARE
isDropped INTEGER;
BEGIN
IF exists(select 1 from pg_constraint where conname = 'my_fk')
THEN
ALTER TABLE my_table DROP CONSTRAINT my_fk CASCADE;
isDropped = 1;
ELSE
isDropped = 0;
END IF;
return isDropped;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION dropfk() OWNER TO postgres;

W dniu 20 kwietnia 2011 13:02 użytkownik Viktor Bojović
<viktor(dot)bojovic(at)gmail(dot)com> napisał:
> use this table:
> http://www.postgresql.org/docs/8.3/static/catalog-pg-constraint.html
> to check if it exists.
> On Wed, Apr 20, 2011 at 12:46 PM, Lukasz Brodziak
> <lukasz(dot)brodziak(at)gmail(dot)com> wrote:
>>
>> Hello,
>>
>> Is there a way of dropping a FK only if it exists. The thing is that I
>> don't know if client has this FK on the table or not.
>>
>> --
>> Łukasz Brodziak
>> "What if everyting around You isn't quite as it seems,
>> What if all the world You think You know is an inelaborate dream
>> When You look at Your reflection is that all you want it to be
>> What if You could look right through the cracks
>> Would You find Yourself...... Find Yourself afraid to see"
>>
>> --
>> Sent via pgsql-admin mailing list (pgsql-admin(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-admin
>
>
>
> --
> ---------------------------------------
> Viktor Bojović
> ---------------------------------------
> Wherever I go, Murphy goes with me
>

--
Łukasz Brodziak
"What if everyting around You isn't quite as it seems,
What if all the world You think You know is an inelaborate dream
When You look at Your reflection is that all you want it to be
What if You could look right through the cracks
Would You find Yourself...... Find Yourself afraid to see"

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message rajibdk 2011-04-20 13:13:32 database system identifier differs between the primary and standby
Previous Message Viktor Bojović 2011-04-20 11:02:50 Re: Dropping foreign key only if it exists