Re: [GENERAL] PostgreSQL: Question about rules

2006-11-17 Thread Jeff Davis
On Fri, 2006-11-17 at 16:42 -0800, Jeremy Smith wrote: > On 11/17/06, Jeff Davis <[EMAIL PROTECTED]> wrote: > > > > Why do you need so many functions though? Won't one SQL function do the > > trick? > > > > Regards, > > Jeff Davis > > > > > > You're right, one will do the trick - I was thi

Re: [GENERAL] PostgreSQL: Question about rules

2006-11-17 Thread Jeremy Smith
On 11/17/06, Jeff Davis <[EMAIL PROTECTED]> wrote: Why do you need so many functions though? Won't one SQL function do the trick? Regards, Jeff Davis You're right, one will do the trick - I was thinking update and delete, but of course those will be fine with plain rules because the

Re: [GENERAL] PostgreSQL: Question about rules

2006-11-17 Thread Jeff Davis
On Fri, 2006-11-17 at 11:49 -0800, Jeremy Smith wrote: > Now, the default value for new.id gets evaluated *each time* I > reference new.id - meaning the rule's first insert sees N for new.id > while the rule's second insert sees N+1. That is kind of odd - I > would think that the default value wo

Re: [GENERAL] PostgreSQL: Question about rules

2006-11-17 Thread Jeremy Smith
On 11/16/06, Tom Lane <[EMAIL PROTECTED]> wrote: Actually, the best way to do that is to attach a default to the view itself. CREATE VIEW v AS SELECT ... ; CREATE RULE ... for insert on v ... ; ALTER TABLE v ALTER COLUMN c DEFAULT whatever; In this formulation the rule

Re: [GENERAL] PostgreSQL: Question about rules

2006-11-16 Thread Tom Lane
Jeff Davis <[EMAIL PROTECTED]> writes: > Interesting question. It seems like you're trying to make a default > value for a view that's based on the underlying table's default value. > I think the normal way to do this is to _not_ have a default value on > the underlying table, and instead use the

Re: [GENERAL] PostgreSQL: Question about rules

2006-11-16 Thread Jeff Davis
On Thu, 2006-11-16 at 16:58 -0800, Jeremy Smith wrote: > > One more thing that would sweeten the deal even further! Not so much > for sequences, but for other columns with default values: > > insert into foo(bar) values(COALESCE(new.bar, DEFAULT)) > > This doesn't work, because DEFAULT is a lan

Re: [GENERAL] PostgreSQL: Question about rules

2006-11-16 Thread Jeremy Smith
On 11/16/06, Jeremy Smith <[EMAIL PROTECTED]> wrote: On 11/16/06, Jeff Davis <[EMAIL PROTECTED]> wrote: > create rule "child_with_parent_explicit_insert" as > on insert to child_with_parent_explicit do instead ( > insert into parent(id, foo) values(COALESCE > ( new.id

Re: [GENERAL] PostgreSQL: Question about rules

2006-11-16 Thread Jeremy Smith
On 11/16/06, Jeff Davis <[EMAIL PROTECTED]> wrote: create rule "child_with_parent_explicit_insert" as on insert to child_with_parent_explicit do instead ( insert into parent(id, foo) values(COALESCE (new.id,NEXTVAL('parent_id_seq')), new.foo); insert into

Re: [GENERAL] PostgreSQL: Question about rules

2006-11-16 Thread Jeff Davis
On Thu, 2006-11-16 at 11:34 -0800, Jeremy Smith wrote: > Example: > > Begin example SQL > create table parent ( > id serial primary key, > foo integer, > ); > > create table child ( > id integer references parent(id) on delete cascade, > bar intege

[GENERAL] PostgreSQL: Question about rules

2006-11-16 Thread Jeremy Smith
A question about rules: I am trying to create a rule which handles inserts on a view, and that rule should insert rows into multiple tables, each one of which contains some of the columns that the view contains. What if one of these tables has, for example, a serial primary key? If I explicitly