Re: INSERT / UPDATE into 2 inner joined table simultaneously
Christopher Swingley <[email protected]> Wed, 6 Mar 2019 12:41:13 -0900
| Newsgroups | gmane.comp.db.postgresql.sql |
|---|---|
| Message-ID | <CAHsw449htrKYSpGa8ZpNh1W+DjZF1=uO3FFGeHvVmK9e2iUEdg@mail.gmail.com> |
Lou, On Wed, Mar 6, 2019 at 12:36 PM Lou <[email protected]> wrote: > The data for both tables needs to be saved at the same time so that the id number of table c can be copied into the c_id field of table p. I think what you want to do is use a transaction (BEGIN), insert the data into table c RETURNING id, then insert the data into table p using the id returned from the first query. If there were no errors, COMMIT the transaction, otherwise ROLLBACK. It's two queries, but because of the transaction, it's happening atomically within the database. Cheers, Chris -- Christopher Swingley Fairbanks, Alaska http://swingleydev.com/ [email protected]