dbTalk Databases Forums  

Help on triggers

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Help on triggers in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Pradeepkumar, Pyatalo
 
Posts: n/a

Default Help on triggers - 08-02-2004 , 12:27 AM







Hi all,

I have 2 tables - table1 and table2. Both the tables are related to each
other. I have written triggers so that if a tuple is inserted into table1,
corresponding values will be inserted into table2. and if a tuple is deleted
from table1 then corresponding tuples are deleted from table2 also. The
delete trigger works fine sometimes and other times it gives the following
error.....

delete from table1 where pointid=30;
ERROR: update or delete on "table1" violates foreign key constraint "$2" on
"table2"
DETAIL: key (pointid) =30 is still referenced from table "pointattributes".

I am doing an application in C. I have written a C-function using libpq API
to delete a tuple from table1. that is I pass a parameter to the function
and it has to delete the tuple whose primarykey value= parameter and also
delete correspoding tuples from table2(due to the trigger). so i have formed
a simple delete command on table2in the C-function. When i compile the
application it gives the same error:

is there anyone who can explain why this is happening. I had posted this
query 2 days back...dint get any reply. Please help me out with this.



Quote:
With Best Regards
Pradeep Kumar P J

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



Reply With Quote
  #2  
Old   
Scott Marlowe
 
Posts: n/a

Default Re: Help on triggers - 08-02-2004 , 03:19 AM






On Sun, 2004-08-01 at 23:27, Pradeepkumar, Pyatalo (IE10) wrote:
Quote:
Hi all,

I have 2 tables - table1 and table2. Both the tables are related to each
other. I have written triggers so that if a tuple is inserted into table1,
corresponding values will be inserted into table2. and if a tuple is deleted
from table1 then corresponding tuples are deleted from table2 also. The
delete trigger works fine sometimes and other times it gives the following
error.....

delete from table1 where pointid=30;
ERROR: update or delete on "table1" violates foreign key constraint "$2" on
"table2"
DETAIL: key (pointid) =30 is still referenced from table "pointattributes".

I am doing an application in C. I have written a C-function using libpq API
to delete a tuple from table1. that is I pass a parameter to the function
and it has to delete the tuple whose primarykey value= parameter and also
delete correspoding tuples from table2(due to the trigger). so i have formed
a simple delete command on table2in the C-function. When i compile the
application it gives the same error:

is there anyone who can explain why this is happening. I had posted this
query 2 days back...dint get any reply. Please help me out with this.
Using foreign keys, you should be able to just set the relation to
cascade and have the dependent rows deleted automagically.


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



Reply With Quote
  #3  
Old   
Pradeepkumar, Pyatalo
 
Posts: n/a

Default Re: Help on triggers - 08-02-2004 , 04:00 AM




I have done that. while creating table2 i have specified ON DELETE CASCADE
for the foreign key constraint.
But I am getting this error when i call the function from a C++ file.



-----Original Message-----
From: Scott Marlowe [mailto:smarlowe (AT) qwest (DOT) net]
Sent: Monday, August 02, 2004 1:49 PM
To: Pradeepkumar, Pyatalo (IE10)
Cc: pgsql-novice (AT) postgresql (DOT) org
Subject: Re: [NOVICE] Help on triggers


On Sun, 2004-08-01 at 23:27, Pradeepkumar, Pyatalo (IE10) wrote:
Quote:
Hi all,

I have 2 tables - table1 and table2. Both the tables are related to each
other. I have written triggers so that if a tuple is inserted into table1,
corresponding values will be inserted into table2. and if a tuple is
deleted
from table1 then corresponding tuples are deleted from table2 also. The
delete trigger works fine sometimes and other times it gives the following
error.....

delete from table1 where pointid=30;
ERROR: update or delete on "table1" violates foreign key constraint "$2"
on
"table2"
DETAIL: key (pointid) =30 is still referenced from table
"pointattributes".

I am doing an application in C. I have written a C-function using libpq
API
to delete a tuple from table1. that is I pass a parameter to the function
and it has to delete the tuple whose primarykey value= parameter and also
delete correspoding tuples from table2(due to the trigger). so i have
formed
a simple delete command on table2in the C-function. When i compile the
application it gives the same error:

is there anyone who can explain why this is happening. I had posted this
query 2 days back...dint get any reply. Please help me out with this.
Using foreign keys, you should be able to just set the relation to
cascade and have the dependent rows deleted automagically.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #4  
Old   
Scott Marlowe
 
Posts: n/a

Default Re: Help on triggers - 08-02-2004 , 11:17 AM



Are you then still trying to delete the dependent row in your function?
That wouldn't make any sense, since the cascading fk should delete it
anyway.

On Mon, 2004-08-02 at 03:00, Pradeepkumar, Pyatalo (IE10) wrote:
Quote:
I have done that. while creating table2 i have specified ON DELETE CASCADE
for the foreign key constraint.
But I am getting this error when i call the function from a C++ file.



-----Original Message-----
From: Scott Marlowe [mailto:smarlowe (AT) qwest (DOT) net]
Sent: Monday, August 02, 2004 1:49 PM
To: Pradeepkumar, Pyatalo (IE10)
Cc: pgsql-novice (AT) postgresql (DOT) org
Subject: Re: [NOVICE] Help on triggers


On Sun, 2004-08-01 at 23:27, Pradeepkumar, Pyatalo (IE10) wrote:
Hi all,

I have 2 tables - table1 and table2. Both the tables are related to each
other. I have written triggers so that if a tuple is inserted into table1,
corresponding values will be inserted into table2. and if a tuple is
deleted
from table1 then corresponding tuples are deleted from table2 also. The
delete trigger works fine sometimes and other times it gives the following
error.....

delete from table1 where pointid=30;
ERROR: update or delete on "table1" violates foreign key constraint "$2"
on
"table2"
DETAIL: key (pointid) =30 is still referenced from table
"pointattributes".

I am doing an application in C. I have written a C-function using libpq
API
to delete a tuple from table1. that is I pass a parameter to the function
and it has to delete the tuple whose primarykey value= parameter and also
delete correspoding tuples from table2(due to the trigger). so i have
formed
a simple delete command on table2in the C-function. When i compile the
application it gives the same error:

is there anyone who can explain why this is happening. I had posted this
query 2 days back...dint get any reply. Please help me out with this.

Using foreign keys, you should be able to just set the relation to
cascade and have the dependent rows deleted automagically.


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.