Re: sub query constraint

From: Yudie Pg <yudiepg(at)gmail(dot)com>
To: Bruno Wolff III <bruno(at)wolff(dot)to>, Dale Sykora <dalen(at)czexan(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: sub query constraint
Date: 2005-03-29 04:37:42
Message-ID: e460d0c05032820375d1fa471@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> One way to do this is to add a write_access column to actions and use
> a constraint to force it to be true.
>Create a UNIQUE key of
> (name, write_access) for user_data and then add a FOREIGN KEY
> reference from (name, write_access) in actions to (name, write_access)
> in user_data.

Yes the name must unique indexed but couldn't force the write_access
to always 'true'.
I may suggest create a trigger function to validate insert to table actions:

CREATE OR REPLACE FUNCTION validate_actions_insert() RETRUNS OPAQUE AS '
DECLARE
rs RECORD;

BEGIN
SELECT INTO rs * FROM user_data WHERE name = NEW.user and write_access = 't';

IF NOT FOUND THEN
RAISE EXCEPTION ''writing access forbidden for user '', NEW.user;
END IF;

RETURN NEW;
END;
' LANGUAGE plpgsql;

CREATE TRIGGER tg_actions BEFORE INSERT OR UPDATE ON actions
FOR EACH ROW EXECUTE PROCEDURE validate_actions_insert();

You may need create another trigger for table user_data before update
for reverse validation.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alex Adriaanse 2005-03-29 05:03:42 Re: Tracking row updates - race condition
Previous Message Bruno Wolff III 2005-03-29 04:15:20 Re: sub query constraint