pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedTrigger triggered from a foreign key
3+ messages / 3 participants
[nested] [flat]
* Trigger triggered from a foreign key
@ 2012-10-19 18:14 Victor Sterpu <victor@caido.ro>
0 siblings, 2 replies; 3+ messages in thread
From: Victor Sterpu @ 2012-10-19 18:14 UTC (permalink / raw)
To: pgsql-sql
I have this trigger that works fine. The trigger prevents the deletion
of the last record.
But I want skip this trigger execution when the delete is done from a
external key.
How can I do this?
This is the fk
ALTER TABLE focgdepartment
ADD CONSTRAINT fk_focgdep_idfocg FOREIGN KEY (idfocg)
REFERENCES focg (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE;
This is the trigger
CREATE FUNCTION check_focgdepartment_delete_restricted() RETURNS trigger
AS $check_focgdepartment_delete_restricted$
BEGIN
IF ( (SELECT count(*) FROM focgdepartment WHERE idfocg =
OLD.idfocg)=1)
THEN RAISE EXCEPTION 'Last record can not be deleted';
END IF;
RETURN OLD;
END;
$check_focgdepartment_delete_restricted$
LANGUAGE plpgsql;
CREATE TRIGGER focgdepartment_delete_restricted BEFORE DELETE ON
focgdepartment FOR EACH ROW EXECUTE PROCEDURE
check_focgdepartment_delete_restricted();
Thank you
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Trigger triggered from a foreign key
@ 2012-10-19 18:24 David Johnston <polobo@yahoo.com>
parent: Victor Sterpu <victor@caido.ro>
1 sibling, 0 replies; 3+ messages in thread
From: David Johnston @ 2012-10-19 18:24 UTC (permalink / raw)
To: 'Victor Sterpu' <victor@caido.ro>; pgsql-sql
> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-
> owner@postgresql.org] On Behalf Of Victor Sterpu
> Sent: Friday, October 19, 2012 2:15 PM
> To: pgsql-sql@postgresql.org
> Subject: [SQL] Trigger triggered from a foreign key
>
> I have this trigger that works fine. The trigger prevents the deletion of
the
> last record.
> But I want skip this trigger execution when the delete is done from a
external
> key.
> How can I do this?
>
I do not think this is possible; there is no "stack" context to examine to
determine how a DELETE was issued.
The trigger itself would seem to be possibly exhibit concurrency issues,
meaning that in certain circumstances the last record could be deleted. You
may want to add explicit locking to avoid that possibility. That or figure
out a better way to accomplish whatever it is you are trying to do.
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Trigger triggered from a foreign key
@ 2012-10-22 11:29 Jasen Betts <jasen@xnet.co.nz>
parent: Victor Sterpu <victor@caido.ro>
1 sibling, 0 replies; 3+ messages in thread
From: Jasen Betts @ 2012-10-22 11:29 UTC (permalink / raw)
To: pgsql-sql
On 2012-10-19, Victor Sterpu <victor@caido.ro> wrote:
> I have this trigger that works fine. The trigger prevents the deletion
> of the last record.
> But I want skip this trigger execution when the delete is done from a
> external key.
> How can I do this?
perhaps you have to use a rule instead of a trigger?
--
⚂⚃ 100% natural
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2012-10-22 11:29 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-10-19 18:14 Trigger triggered from a foreign key Victor Sterpu <victor@caido.ro>
2012-10-19 18:24 ` David Johnston <polobo@yahoo.com>
2012-10-22 11:29 ` Jasen Betts <jasen@xnet.co.nz>
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