X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 9958CD1B49B for ; Fri, 16 Jan 2004 05:19:04 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 56325-05 for ; Fri, 16 Jan 2004 01:18:33 -0400 (AST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id D4E5DD1DA9B for ; Fri, 16 Jan 2004 01:18:32 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 426D93564F; Thu, 15 Jan 2004 21:18:32 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 412BF3564A; Thu, 15 Jan 2004 21:18:32 -0800 (PST) Date: Thu, 15 Jan 2004 21:18:32 -0800 (PST) From: Stephan Szabo To: Denis Cc: pgsql-sql@postgresql.org Subject: Re: Initially Deffered - FK In-Reply-To: <006601c3dbea$02295800$0f32a8c0@denisnew> Message-ID: <20040115211617.C53092@megazone.bigpanda.com> References: <006601c3dbea$02295800$0f32a8c0@denisnew> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/134 X-Sequence-Number: 16527 On Fri, 16 Jan 2004, Denis wrote: > create table contact (id int constraint contact_pk primary key, name > text ); > create table address (id int constraint address_fk references contact(id) on > delete cascade initially deferred, > city text, > pin text); > > Lets.. insert few data in it.. > > insert into contact values (1, 'Denis'); > insert into contact values (2, 'Anand'); > insert into contact values (3, 'Debatosh'); > insert into contact values (4, 'Pradeep'); > > insert into address values (1,'Howrah','711102'); > insert into address values (2,'Kolkata','700001'); > insert into address values (3,'Jadavpur','700005'); > insert into address values (4,'Mumbai','400002'); > > Now, below gives me the correct result. > > select * from contact; select * from address; > > acedg=> select * from contact; select * from address; > id | name > ----+---------- > 1 | Denis > 2 | Anand > 3 | Debatosh > 4 | Pradeep > (4 rows) > > id | city | pin > ----+----------+-------- > 1 | Howrah | 711102 > 2 | Kolkata | 700001 > 3 | Jadavpur | 700005 > 4 | Mumbai | 400002 > (4 rows) > > BUT, the problem starts when i issue the following set of DMLs in > transaction: > > begin; > delete from contact where id=1; > insert into contact values (1, 'Denis'); > delete from address where id=1; /* this is not required.. but my > app.fires. Should not have any impact */ > insert into address values (1,'Howrah','711102'); > end; > > It gives me the result: > > acedg=> select * from contact; select * from address; > id | name > ----+---------- > 2 | Anand > 3 | Debatosh > 4 | Pradeep > 1 | Denis > (4 rows) > > id | city | pin > ----+----------+-------- > 2 | Kolkata | 700001 > 3 | Jadavpur | 700005 > 4 | Mumbai | 400002 > (3 rows) > > Where is my lastly inserted row ?? i.e. > insert into address values (1,'Howrah','711102'); Definitional difference. We currently treat a request to defer the constraint to mean defer referential actions as well, thus the inserted address is removed when the on delete cascade occurs after it at transaction end. Noone's been entirely sure whether this is correct or not per spec as I remember.