Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWecD-0003Ty-75 for pgsql-novice@arkaria.postgresql.org; Wed, 14 Apr 2021 12:27:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lWecC-0000FV-4P for pgsql-novice@arkaria.postgresql.org; Wed, 14 Apr 2021 12:27:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWecB-0000Cr-St for pgsql-novice@lists.postgresql.org; Wed, 14 Apr 2021 12:27:27 +0000 Received: from mout.gmx.net ([212.227.17.20]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lWec7-0006VP-L3 for pgsql-novice@lists.postgresql.org; Wed, 14 Apr 2021 12:27:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1618403242; bh=+ZBkVirMZISKi17ZlwcTPvcTV5Ph5q4htx7MKWcbwdA=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=b1TCkZhZa6Dr5YYjYSXz7gtrcgVw2oZPnZFOkDlQRsywOE/9H9rjN2cV9pPHqedk7 MRwT/9PZygXIAkKfMVL5XvwzmKjYrG5B8iD0QeDi1/vwGXxQM3yLsWE4gI1x2ppdaA ltvLd2La3H/0gjlNawjqT4twI0OElUjIM4RlcSZU= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from mail.defcon1.lan ([93.15.31.113]) by mail.gmx.net (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MZCfJ-1l24vz0IwT-00V97v; Wed, 14 Apr 2021 14:27:22 +0200 Date: Wed, 14 Apr 2021 14:27:19 +0200 From: Bzzzz To: pgsql-novice@lists.postgresql.org Cc: steve@tusol.co.uk Subject: Re: Advice on foreign key and cascading delete design - postgresql 12.6 Message-ID: <20210414142719.181ffd62@msi.defcon1.lan> In-Reply-To: References: <3190280.1618325238@sss.pgh.pa.us> <20210413180841.3ba6e741@msi.defcon1.lan> Organization: Anyone, anywhere but in banana demokratik republik of france or UERSS. Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:gcoizL6tUeas8k/MORocovt6ehg8Lye8NRLj5OswkyigDvzGvo5 81QOiStkpsSKVW07FGFrGjQxpxY/eQlDejXF1Dmh5NyShyt3EPYuQIKxVOmJV1pydSA92vL dvh9IAna/HEhHGKMguJnJOGDZukCypmfScMWXcfUkWCUPHESUWZD2ZvlU/wKs6hPH3EprNu 237PtS6JQfEXEg2otFF7A== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:F835B6kHZIk=:Cwg7m3FrYx5pdS60f28Tmm 9Dd5AXcVFurzw/FnqVoCY2BW7m1TxVS4FbeytxjEtVKY9VSIGgqTZILxRLCYJwXe9Zs8L8lq2 bgWa0uVRzoy9txT3M4JEwAEH8SGsWfrXx5HLHJ1+omrcAvDVn8OpVN5ouTmodIb0C4Rkw64fw yfd7d8/VpMyGmVXzu/DQeEXFHajC83MeQK8CiWW1AyM9qFELf3swi86ls+6Dal8/0gY4sgJbh NcR8PSdpYUtHEOwi/fvhqoTAkfmhrqv5zutnZL3l6DgrPVKRGo/KOKti2hxp7SAK4oFWBo9n2 YCkqp40XsrTy7bAWLN8SbnkwoZYgivHU02whiQbH3STBKQ5dsF70r27BxIqgMKfYiJcJ1oDYO kv0mvjvzakDMhW1JCiDMvFyJePHOo5ywinPMeaMlV6MK/6Y+HcPRp2da6py2ycfGL/jHs/e2+ muXR7vSdr9q1kb6uzeuPsPP7sNCjXal+vXpIz4ROCqC2oGh52xFbguvO7XPF5BNowG96t7Jpc nBMAI1K6fuqKw4aXHtfevhcGeO8Sui2P4xi4y/ics85KMYRCDxroxH1lcnU6+TYIWL6F/p502 bQPYnjVA1Prh/1IQ8Z+YuRFyoto3evyNPa/jyvZlH9RPFB5M3hn0hXUqVOtQfZgHhtI+nJN0/ DiNmmGbOMaPjArq4918YQeNyjbK0lhGEx4Qe8SyCVbkVUQ0zqu+VSRMqrQefsHPaeIZvxAg1+ fLOxl5ILT93YKeQfRS235mDZ/ZEGWt7oa08YqkWuZcadR/N8hPqfHKCpT7VaJM8IRqtGEb4uC fhZ1lZfkiu+L+w+KKcS4EbP1nhTsRxmVDRntmn4HmHYIyAqE6dQFR0aIwygfZzKP+0OFHXWYK VI/MWZTYJvUNHHmcAmHCDvWgAEOiY62FF8MZ8uJLbObKw7Oqg1dbV9nwrl+/v+dWNzeLsVArF 3tk9w8rrM0pVU7LN84VG1o5z0jzx6GqnwpLxqFhhjAfbzX/h92dYaK1eC3rDo0RiUKrNG1XkK 4HJiSLvFDsksNwVZNgss9Z+LtlNV6Y/FZ0K8DEelIoRX1PBt7jHrwBhPEcYEvMaRnZE4HmMf6 VB0ag6jT7zMH9zKjiNj/uoYo6D/Z6j55X9H39m3gI/MkhwA/bZUOvL/FWc6KA+jQ9GbQkgptw 0ULBUinOo1SAZ+7CcnaVTrgns11xvih99UhLozsAnv0Ilqb7jXUNhmtxp1JFUVksdoBBg= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 14 Apr 2021 07:50:45 +0100 "Steve Tucknott (TuSol)" wrote: > On Tue, 2021-04-13 at 18:08 +0200, Bzzzz wrote: > > main ( > > id int generated always as identity primary key, > > tm text not null > > ) > >=20 > >=20 > > dependency ( > > id int generated always as identity primary key, > > td text not null, > > ri_main int not null > > REFERENCES public.main ON DELETE CASCADE > > ) > >=20 > >=20 > > This way, when you have a row in 'dependency' that references another > > one > > from 'main', if the 'main' row is deleted, the 'dependency' row is > > automatically also deleted. >=20 > Jean-YvesI think I understand ON DELETE CASCADE and use it on other > tables,but I still don't see how that works with the 'main', > 'dependency' structure (and my notes). That seems to me to only work > if the 'dependency' is only used by 'main', but what if I have other > main tables called 'main2', 'main3', 'main4'...that also have records > on 'dependency'? Oh I missed that, ok, in this case, you need to do 2 things : * add "join" tables with only 2 columns : * a RI to 'main1' (or main2, etc, one table per father) * a RI to 'dependency' with ON DELETE CASCADE | create table schema.main1_dependency( int not null references schema.main1(id) on delete cascade int not null references schema.dependency(id) ) * a trigger for each of these tables that'll delete the concerned 'dependency' rows when a row is deleted from one of these tables (of course, that means you _must_ embed all this into a transaction.) | Note that, if it is what you're looking for, you should be able to=E2=80= =A6 cascade the deletion - ie: you delete a row from 'main5', which, by cascade will delete all pointing rows from a 'main5_dependency' and the trigger will, at last, delete pointing rows from 'dependency'. Looks like that : main1 <--- main1_dependency ---> dependency main2 <--- main2_dependency ---> dependency =E2=80=A6 Creation: [if row doesn't exist into 'main1', transaction may starts here] * create row into 'main1' (ie: 4798) [if row does exist into 'main1', transaction starts here] * create row into 'dependency' (ie: 15025) * create row into 'main1_dependency' to materialize the link (ie: ri_main1=3D2798 & ri_dependency=3D15025) [commit|rollback] Caution: if all of these steps are in only one transaction, you'll have to defer to avoid errors ! Deletion: delete from 'main1' where id=3D4798 CASCADE, deleting from 'main1_dependency' where ri_main1=3D4798 which launch trigger that will delete all rows from 'dependency' when main1_dependency(ri_main1)=3D4798 > My structure handles that as the 'dependency' table > also carries the table name of the 'main' - so I could have rows on > dependency of:1,'some text for main', 1,'main'2,'some text for > main2',1,'main2'3,'some text for main3',1,'main3'.....etcBut the the FK > back to main[123] is composed of two parts - the ID and the table > name.=20 Bad design, because that means you must have a RI column for each and every 'mainN' table into 'dependency' - will works if N < 10, but will start to be a mess when N > 20, unusable if N =E2=89=A5 1000. > I feel that I'm being really stupid here and not understanding what I'm > being told! Apologies if I haven't grasped what you're telling me. No no, my bad, I missed multiple 'mainN' tables. > The only answer I can see is to have a column on every table that may > carry a note, document, address etc that is its table name. But that > seems wrong somehow. > Regards,Steve Jean-Yves