Re: Multiple inserts with two levels of foreign keys

2023-10-08 Thread Dow Drake
Thanks Peter! I'll take a close look at your suggestion when I get a chance. But I've already implemented a Python script that solves my actual problem based on the pattern that Alvaro Herrera suggested for the toy problem I described here. It's working very well to reproduce the farm with sever

Re: Multiple inserts with two levels of foreign keys

2023-10-08 Thread Peter J. Holzer
On 2023-10-05 09:59:24 -0500, Ron wrote: > But honestly, the amount of text duplication hurts my "inner programmer".  > And it would have to be generated dynamically, since you don't know how many > crops were delivered.  #shudder Yes, this seems like the kind of problem that I would definitely so

Re: Multiple inserts with two levels of foreign keys

2023-10-05 Thread Ron
But honestly, the amount of text duplication hurts my "inner programmer".  And it would have to be generated dynamically, since you don't know how many crops were delivered.  #shudder On 10/5/23 09:33, Dow Drake wrote: Yes! Thanks, Alvaro! This is exactly the pattern I was trying to work out

Re: Multiple inserts with two levels of foreign keys

2023-10-05 Thread Dow Drake
Yes! Thanks, Alvaro! This is exactly the pattern I was trying to work out! This community is awesome! > On Oct 5, 2023, at 2:39 AM, Alvaro Herrera wrote: > > On 2023-Oct-04, Dow Drake wrote: > >> I want to insert a farm record, then insert two crops associated with that >> farm, then inser

Re: Multiple inserts with two levels of foreign keys

2023-10-05 Thread Alvaro Herrera
On 2023-Oct-04, Dow Drake wrote: > 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: If I understand you correctly, for each table you want one CTE with the da

Re: Multiple inserts with two levels of foreign keys

2023-10-04 Thread Ron
Have you considered writing a stored procedure to process records that have been written to temporary tables? 0. Create temporary tables tmp_farms, tmp_crops and tmp_deliveries, which don't have id columns. 1. Truncate the three temporary tables 2. Insert into the temp tables a "set" of prod d

Re: Multiple inserts with two levels of foreign keys

2023-10-04 Thread Dow Drake
I see. That would definitely work, but part of this for me is to get a better understanding of PostgreSQL's capabilities. I'm going to keep working on a minimal solution that deletes no records from the dev database, and only inserts the required records. On Wed, Oct 4, 2023 at 6:58 PM Ron wrot

Re: Multiple inserts with two levels of foreign keys

2023-10-04 Thread Ron
Ah.  We'd truncate all of the dev tables, then load a "slice" (for example, accounts 1 to 1, 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

Re: Multiple inserts with two levels of foreign keys

2023-10-04 Thread Dow Drake
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 ke

Re: Multiple inserts with two levels of foreign keys

2023-10-04 Thread Ron
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 databas

Multiple inserts with two levels of foreign keys

2023-10-04 Thread Dow Drake
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