foreign key violation

From: "surabhi(dot)ahuja" <surabhi(dot)ahuja(at)iiitb(dot)ac(dot)in>
To: <pgsql-general(at)postgresql(dot)org>
Subject: foreign key violation
Date: 2006-05-26 10:41:44
Message-ID: 8626C1B7EB748940BCDD7596134632BE398684@jal.iiitb.ac.in
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

i have 2 tables.

the first table "TAB1", has a primary key "id", which is the foreign key in the second table "TAB2"

i have 2 stored procedures,
1 stored procedure does

check if there is a row already present in TAB1 with col 2 = $1;
if not present
then
insert into TAB1(col2) values ($1 );
select into "myid" tab1_id from TAB1 where col2 = $1;
return myid
end if

please note that tab1_id is a serial bigint.

second stored procedure does

check if there is a row already present in TAB2 with col 2 = $2;
if not present
then
insert into TAB2 (col2, col3) values ($1 , $2);
select into "myid" tab2_id from TAB2 where col2 = $1;
return myid
end if


please note that tab2_id is the seriesl bigint.
also the second argument $2, is the value for the field col3, which is THE FOREIGN KEY.

in my transaction i do the following

begin
id = call stored procedure 1("abc");
id2 = call stored procedure 2 ("something", id);
commit

however i may have another transaction going in parallel, where i do this

delete from TAB1 where tab1_id = 5; //5 is just some value

in such a case sometime a foreign key violation occurs when the stored procedure 2 is called.

Is there any way i can avoid this problem?

thanks,
regards
Surabhi

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Leonel Nunez 2006-05-26 11:11:13 Re: latest release of 8.0.8
Previous Message Alban Hertroys 2006-05-26 09:25:37 Re: [SQL] (Ab)Using schemas and inheritance