Re: Conditional rule? - Mailing list pgsql-sql

From André Næss
Subject Re: Conditional rule?
Date
Msg-id 00f001bff876$cc57f8d0$74b5f8c2@wkst6
Whole thread Raw
In response to Re: Conditional rule?  (JanWieck@t-online.de (Jan Wieck))
List pgsql-sql
----- Original Message -----
>     No. The rule
>
>         CREATE RULE newsrule AS ON INSERT TO news
>             WHERE new.publishtime NOTNULL DO
>             INSERT INTO news_unpublished VALUES (new.id);
>
>     should  do  the  job  perfectly.  Maybe  you want to have the
>     following rules too:

The following happens:
rules=# insert into news (title, time) values('Hei', now());
ERROR:  <unnamed> referential integrity violation - key referenced from
news_unpublished not found in news

Seems the data is inserted into news_unpublished first, thereby violating
the constraint I have defined for the news_id field (see below). After
removing the constraint a second problem arose; the id created for news
(serial) was 4, while the id inserted into news_unpublished was 3. So far a
trigger procedure seems to be the best solution.

>         CREATE RULE newsrule4 AS ON DELETE TO news
>             WHERE old.publishtime NOTNULL DO
>             DELETE FROM news_unpublished WHERE news_unpublished.id =
old.id;

This is also achieved by the following right? (id is the primary key for
news):
create table news_unpublished ( news_id int references news on delete cascade
);


André Næss



pgsql-sql by date:

Previous
From: Carolyn Lu Wong
Date:
Subject: Transactions
Next
From: "André Næss"
Date:
Subject: Re: BLOBs