plpgsql function and constraints

From: Jakub Ouhrabka <jouh8664(at)ss1000(dot)ms(dot)mff(dot)cuni(dot)cz>
To: <pgsql-general(at)postgresql(dot)org>
Subject: plpgsql function and constraints
Date: 2001-08-30 18:55:10
Message-ID: Pine.LNX.4.33.0108302037470.9002-100000@u-pl0
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

hi,

i have table1 and table2. in table1 there is a foreign key referencing id
in table2. i'd like to do something like this:

CREATE FUNCTION foo() RETURNS INTEGER AS '
DECLARE
BEGIN
UPDATE table1 SET not_key = 100;
DELETE FROM table1;
DELETE FROM table2;
RETURN 0;
END;
' LANGUAGE 'plpgsql';

when i run select foo(); i always get:

ERROR: <unnamed> referential integrity violation - key referenced from
table1 not found in table2

(i think that's because constraints are checked at the end of statement
and this function is treated as one statement... or am i wrong?)

when i try it in psql as separate commands in one transaction it's ok:
BEGIN;
UPDATE table1 SET not_key = 100;
DELETE FROM table1;
DELETE FROM table2;
COMMIT;

is there any way to do something like this in plpgsql function?

thanks in advance,

kuba

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Marc André Paquin 2001-08-30 20:26:18 2 tables, joins and same name...
Previous Message Marshall Spight 2001-08-30 18:51:44 Re: PL/java?