X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id DEA519F9D99 for ; Mon, 22 May 2006 16:33:59 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 11467-10 for ; Mon, 22 May 2006 16:33:50 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 85A4C9F9B01 for ; Mon, 22 May 2006 16:33:50 -0300 (ADT) Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by svr4.postgresql.org (Postfix) with ESMTP id 12F675B09D5 for ; Mon, 22 May 2006 19:31:45 +0000 (GMT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1FiG7b-00024G-P6 for pgsql-admin@postgresql.org; Mon, 22 May 2006 21:31:20 +0200 Received: from pirus.securax.be ([213.16.46.130]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 22 May 2006 21:31:19 +0200 Received: from postgres by pirus.securax.be with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 22 May 2006 21:31:19 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-admin@postgresql.org From: Kouber Saparev Subject: Re: Updatable view and default sequence values Date: Mon, 22 May 2006 22:30:47 +0300 Lines: 54 Message-ID: References: <20060522165026.GV64371@pervasive.com> Reply-To: postgres@saparev.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: pirus.securax.be User-Agent: Mozilla Thunderbird 1.5.0.2 (Windows/20060308) In-Reply-To: <20060522165026.GV64371@pervasive.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/294 X-Sequence-Number: 21826 Jim C. Nasby wrote: > On Tue, May 16, 2006 at 04:16:55PM +0300, Kouber Saparev wrote: >> The tricky part comes when I try to make my view insertable. Normally >> I'd insert without specifying the sequence values, but in some cases I'd >> want also to have full control of what's going into the underlying >> tables. The thing is that when I try to do it the simple way by >> specifying default values in the view itself: >> >> ALTER TABLE s_purchase ALTER COLUMN purchase_sid SET DEFAULT >> NEXTVAL('purchase_purchase_sid_seq'); >> ALTER TABLE s_purchase ALTER COLUMN subscription_purchase_sid SET >> DEFAULT NEXTVAL('subscription_purchase_subscription_purchase_sid_seq'); > > You're doing ALTER TABLE on a view? Exactly, AFAIK there's no ALTER VIEW command. >> CREATE RULE s_purchase_insert AS >> ON INSERT TO s_purchase DO INSTEAD ( >> INSERT INTO purchase >> (purchase_sid, data) >> VALUES >> (NEW.purchase_sid, NEW.pdata); >> >> INSERT INTO subscription_purchase >> (subscription_purchase_sid, purchase_sid, data) >> VALUES >> (NEW.subscription_purchase_sid, NEW.purchase_sid, NEW.sdata); >> ); > > Why not just use CURRVAL('purchase_purchase_sid_seq') in the rule? Because I would like to be able to insert data both by specifying and without specifying values for primary keys. For example: INSERT INTO s_purchase (pdata, sdata) VALUES ('x', 'y'); INSERT INTO s_purchase (purchase_sid, pdata, subscription_purchase_sid, sdata) VALUES (123, 'x', 456, 'y'); If I specify CURRVAL and not NEW.primary_key, as you're proposing, I will lose the second way of adding data, cause in the latter case the values have nothing to do with the sequences, hence CURRVAL will give me completely useless, or even worse - wrong data. That's why I'm using default values of a view - if there's a value provided, it will be entered as is, if not - then the default value (nextval in this case) will be taken. However, this solution is not robust enough. That's why I'm looking for other possible solutions. :) -- Kouber Saparev http://kouber.saparev.com