Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qoDcQ-000XAH-H3 for pgsql-general@arkaria.postgresql.org; Thu, 05 Oct 2023 01:57:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qoDcM-007hj9-Sc for pgsql-general@arkaria.postgresql.org; Thu, 05 Oct 2023 01:57:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qoDcM-007hj1-FC for pgsql-general@lists.postgresql.org; Thu, 05 Oct 2023 01:57:35 +0000 Received: from mail-yb1-xb2f.google.com ([2607:f8b0:4864:20::b2f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qoDcK-0005fd-Hl for pgsql-general@lists.postgresql.org; Thu, 05 Oct 2023 01:57:34 +0000 Received: by mail-yb1-xb2f.google.com with SMTP id 3f1490d57ef6-d849df4f1ffso556780276.0 for ; Wed, 04 Oct 2023 18:57:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696471050; x=1697075850; darn=lists.postgresql.org; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=sYBuQhjD0Ai9NUEGwl3yy89cBIwcUjT1Cts/2YuC0+w=; b=Bs7zjuqTGrvJe9nigWHMejerlMuNoFjJnLDylIO4ZRBXJtGpVSyznqZOHMtOCzE+BD 0IEf11H+GQ7QXR+Ew3zMjVXcLlH8/xtgTSAPQI/vKJUnLrfnVfSdELHNpj3zF0B4rXQ6 hmq7G6rWVKPaROLJ0TuIxT+tlhjYVa2PVcFi1FESxYJomb8Z67oW28XBiG5Tqqq/dwSq KNSHz3OINSRqo3pAbhaUrslGUYHpVEDGUPS6OKWSCl4IWcnQzuyUv3psR7NvCTFnMXcb Hz74936QItGBpBTdmC7aACIJjcDzF0N33BX42zH6G9HB/BS7mpeSz8T9QCKH01yweGYc mqaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696471050; x=1697075850; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=sYBuQhjD0Ai9NUEGwl3yy89cBIwcUjT1Cts/2YuC0+w=; b=TeW607/GbI/zy5RUyEtH6D7ytyjY3+SFo8FXqeTO9ItdifZEsYcdhTyUxcXqCiVZR8 eb7PH75zW6L8d+dwG3jeTEsjNSqB7pDaUUTN9ZxAVQrT42uvg4+igz2exmq5mpNzlW+4 BvOw3C/dCG1k97MTyM/I6udfkz/L+dJEYz9EXz1VVtnfeVy2XXaIKkk7OQaVf271JasP RBc/eVz5LkGrGkFnft3RxiHi7j18HR9zaG7ybCtmbuM1ZkHCpwi5wW6UfDjde6RuNNno 3bp5Dv2SvKSFpNT5qwewpkUCGHAFSUBhuOKlfuthb6OY5mu1uaqlM55eDRnMctN10mNl ZS4w== X-Gm-Message-State: AOJu0YyvF908CazPzxD5KfhSoefFMmtfXpjiC50qlH03fmc4rcPpgMrG 59xHDOPv+9oJ0KloI4PQzgjOqBchDDw= X-Google-Smtp-Source: AGHT+IGomyrTtpGRywTcvzPbcDrmWYX0xHEtIkANIuuq2eWUN/BuPgG/93emnF4n/pOOfJVQQNI/vQ== X-Received: by 2002:a25:8e06:0:b0:d44:af:3cce with SMTP id p6-20020a258e06000000b00d4400af3ccemr3731553ybl.27.1696471049640; Wed, 04 Oct 2023 18:57:29 -0700 (PDT) Received: from ?IPV6:2600:1700:bd2c:b810:25a5:fc13:c829:adda? ([2600:1700:bd2c:b810:25a5:fc13:c829:adda]) by smtp.googlemail.com with ESMTPSA id b33-20020a25aea1000000b00d8179f577basm102746ybj.49.2023.10.04.18.57.29 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Oct 2023 18:57:29 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------wUjuyE30d2KzVGZprKktF0zl" Message-ID: Date: Wed, 4 Oct 2023 20:57:29 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: Multiple inserts with two levels of foreign keys Content-Language: en-US To: pgsql-general@lists.postgresql.org References: From: Ron In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------wUjuyE30d2KzVGZprKktF0zl Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Ah.  We'd truncate all of the dev tables, then load a "slice" (for example, accounts 10000 to 19999, and all associated records from downstream tables; lots and lots of views!!) from the prod database. On 10/4/23 20:50, Dow Drake wrote: > Thanks for the reply, Ron! > I'm not sure I see how to make your suggestion work, though.  Suppose I > dump the three tables to CSV as you suggest (and write a script to extract > the relevant records from those CSV dumps in the correct order).  It might > be that in the dev database, the next generated key values are 199 for > farm's id, 2145 for crop's id and 10242 for deliveries' id.  The databases > are independent. > > Just inserting the records in the same order doesn't take care of setting > the foreign key values correctly -- does it? I think I'm really looking > for a solution more along the lines of the link in my original post. > > Best, > Dow > > On Wed, Oct 4, 2023 at 6:26 PM Ron wrote: > > Frame challenge: why can't you just "\copy to" the dev database tables > in the correct order, to satisfy foreign key requirements? > > On 10/4/23 18:59, Dow Drake wrote: >> Hi, >> >> I'm trying to write a postgresql script to replicate a hierarchical >> structure in a live database into my development database, where I >> can debug and test more easily.  I can extract the data from the live >> database that needs to be inserted, but I'm having trouble writing >> the insertion script >> >> Here's a simplified version of the problem I'm trying to solve: >> There are three tables: farms, crops and deliveries where a farm has >> many crops and a crop has many deliveries. >> >> create table farms ( >>    id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, >>    name character varying(30) >> ); >> create table crops ( >>    id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, >>    farm_id bigint not null >>    name character varying(30) >> ); >> create table deliveries ( >>    id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, >>    crop_id bigint not null >>    ticket character varying(30) >> ); >> I want to insert a farm record, then insert two crops associated with >> that farm, then insert two deliveries for each of the the two crops >> so that in the end, my tables look like this: >> farms >> id     name >> 1 'Happy Valley Farm' >> >> crops >> id farm_id    name >> 1 1         'corn' >> 2 1         'wheat' >> >> delvieries >> id crop_id    ticket >> 1 1          '3124' >> 2 2          '3127' >> 3 1          '3133' >> 4 2          '3140' >> >> It's important that the deliveries get assigned to the right crops.  >> I think this post: https://dba.stackexchange.com/questions/199916 >> gets close to what I need, but I haven't been able to figure out how >> to adapt it to multiple records. >> >> Thanks for any help on this! >> >> > > -- > Born in Arizona, moved to Babylonia. > -- Born in Arizona, moved to Babylonia. --------------wUjuyE30d2KzVGZprKktF0zl Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Ah.  We'd truncate all of the dev tables, then load a "slice" (for example, accounts 10000 to 19999, and all associated records from downstream tables; lots and lots of views!!) from the prod database.

On 10/4/23 20:50, Dow Drake wrote:
Thanks for the reply, Ron!
I'm not sure I see how to make your suggestion work, though.  Suppose I dump the three tables to CSV as you suggest (and write a script to extract the relevant records from those CSV dumps in the correct order).  It might be that in the dev database, the next generated key values are 199 for farm's id, 2145 for crop's id and 10242 for deliveries' id.  The databases are independent.

Just inserting the records in the same order doesn't take care of setting the foreign key values correctly -- does it?  I think I'm really looking for a solution more along the lines of the link in my original post.

Best,
Dow

On Wed, Oct 4, 2023 at 6:26 PM Ron <ronljohnsonjr@gmail.com> wrote:
Frame challenge: why can't you just "\copy to" the dev database tables in the correct order, to satisfy foreign key requirements?

On 10/4/23 18:59, Dow Drake wrote:
Hi,

I'm trying to write a postgresql script to replicate a hierarchical structure in a live database into my development database, where I can debug and test more easily.  I can extract the data from the live database that needs to be inserted, but I'm having trouble writing the insertion script

Here's a simplified version of the problem I'm trying to solve:
There are three tables: farms, crops and deliveries where a farm has many crops and a crop has many deliveries.

create table farms (
   id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
   name character varying(30)
);
create table crops (
   id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
   farm_id bigint not null
   name character varying(30)
);
create table deliveries (
   id bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
   crop_id bigint not null
   ticket character varying(30)
);
I want to insert a farm record, then insert two crops associated with that farm, then insert two deliveries for each of the the two crops so that in the end, my tables look like this:
farms
id     name
1      'Happy Valley Farm'

crops
id     farm_id    name
1        1         'corn'
2        1         'wheat'

delvieries
id       crop_id    ticket
1         1          '3124'
2         2          '3127'
3         1          '3133'
4         2          '3140'

It's important that the deliveries get assigned to the right crops.  I think this post: https://dba.stackexchange.com/questions/199916
gets close to what I need, but I haven't been able to figure out how to adapt it to multiple records.

Thanks for any help on this!



--
Born in Arizona, moved to Babylonia.

--
Born in Arizona, moved to Babylonia.
--------------wUjuyE30d2KzVGZprKktF0zl--