Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwnYI-0001WV-Pv for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2019 13:06:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hwnYH-0004SF-C1 for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2019 13:06:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwnYG-0004IR-91 for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2019 13:06:24 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hwnYD-0002yV-Ob for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2019 13:06:23 +0000 Received: from panix2.panix.com (panix2.panix.com [166.84.1.2]) by mailbackend.panix.com (Postfix) with ESMTP id 465zkg5XgKz1Q08 for ; Sun, 11 Aug 2019 09:06:19 -0400 (EDT) Received: by panix2.panix.com (Postfix, from userid 17622) id 465zkg4Nsbz1ZVM; Sun, 11 Aug 2019 09:06:19 -0400 (EDT) Date: Sun, 11 Aug 2019 09:06:19 -0400 From: stan To: pgsql-general@lists.postgresql.org Subject: Re: A question about trigger fucntion syntax Message-ID: <20190811130619.GA26219@panix.com> References: <20190811125613.GA7408@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190811125613.GA7408@panix.com> X-Editor: gVim User-Agent: Mutt/1.12.1 (2019-06-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Aug 11, 2019 at 08:56:13AM -0400, stan wrote: > Good morning (at least is is morning East Coast USA time). > > I am trying to create a function to validate an attempted record > insert, and I am having a hard time coming up with syntax that > is acceptable. > > Here is the scenario I have a table that has (among other items) employee_key > and work_type_key (both integer FOREIGN KEYS). Then I have another table > that has the following structure: > > CREATE TABLE permitted_work ( > employee_key integer , > work_type_key integer , > permit boolean DEFAULT FALSE NOT NULL , > modtime timestamptz DEFAULT current_timestamp , > FOREIGN KEY (employee_key) references > employee(employee_key) , > FOREIGN KEY (work_type_key) references > work_type(work_type_key) , > CONSTRAINT permit_constraint UNIQUE > (employee_key , work_type_key) > ); > > What I think I need to do is create a function that is fired on an insert, > or update to the 1st table that verifies that there is an existing row in > permitted_work that matches the combination of employee_key AND > work_type_key AND has the value TRUE in the permit column. > > First does this seem to be a good way to achieve this constraint? If not, > I am open to suggestions as to other ways to address this requirement. > > If it does, could someone give me a little help with th syntax of the > needed function ?? > > Thanks for your time helping me with this. BTW, here is what I Ave tried. CREATE OR REPLACE FUNCTION check_permission() RETURNS trigger AS $BODY$ BEGIN SELECT permit FROM permitted_work WHERE NEW.employee_key = OLD.employee_key AND NEW.work_type_key = OLD.work_type_key RETURN permit; END; $BODY$ LANGUAGE PLPGSQL; and when I try to insert it I get a syntax error at the RETURN -- "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin