X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 680933A410E for ; Wed, 10 Nov 2004 03:19:46 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 75634-02 for ; Wed, 10 Nov 2004 03:19:36 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 2A0F53A40AD for ; Wed, 10 Nov 2004 03:19:39 +0000 (GMT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 5C340358AA; Tue, 9 Nov 2004 19:19:37 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 5A84E3587B; Tue, 9 Nov 2004 19:19:37 -0800 (PST) Date: Tue, 9 Nov 2004 19:19:37 -0800 (PST) From: Stephan Szabo To: "Ferindo Middleton, Jr" Cc: pgsql-sql@postgresql.org Subject: Re: UPDATE/INSERT on multiple co-dependent tables In-Reply-To: <000c01c4c6ce$03da7e70$1001a8c0@RENNYSLAPTOP> Message-ID: <20041109191724.F63596@megazone.bigpanda.com> References: <000c01c4c6ce$03da7e70$1001a8c0@RENNYSLAPTOP> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/94 X-Sequence-Number: 19413 On Tue, 9 Nov 2004, Ferindo Middleton, Jr wrote: > Is it possible for an UPDATE/INSERT query string to function in such a way > that it requires two like fields in different tables to be equal to/'in sync > with' one another: > > Example: I have two tables: registration & schedules.... > they both record a class_id, start_date, end_date... I want to make sure > that if the schedule_id field is updated in the registration table; that > class_id, start_date & end_date fields automatically change to match the > schedules.id record in the schedules table.... I've devised a function to > handle this but pgsql recognizes the query to be 'infinitely recursive: > > CREATE RULE registration_update AS > ON UPDATE TO registration > DO > UPDATE registration SET class_id = schedules.class_id WHERE new.schedule_id > = (SELECT schedules.id FROM schedules WHERE id = new.schedule_id); > > What I'm doing is kind of redundant but necessary for > backwards-compatibility You would probably have better luck doing something like the above in a before trigger rather than a rule by having the before trigger change NEW.class_id to the desired value.