How to script inserts where id is needed as fk

From: Michael Schmidt <css(dot)liquid(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: How to script inserts where id is needed as fk
Date: 2013-11-08 19:44:42
Message-ID: 527D3F2A.6060504@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi guys,

i need to script some insert statements. To simplify it a little bit so
assume i got a

table "User" and a table called "Article". Both tables have an serial id
column. In "Articles" there is a column i need to fill with the user id
lets call it "create_user_id".

I want to do something like:

Insert into User (name) values ('User1');

Insert into Article ('create_user_id') values (1);
Insert into Article ('create_user_id') values (1);

Insert into User (name) values ('User2');

Insert into Article ('create_user_id') values (2);
Insert into Article ('create_user_id') values (2);

So you see i have set it to 1 and 2 this not good cause it might not be
1 and 2.
I probably need the id returned by the "insert into User" query to use
it for the "insert into Article" query.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jonathan S. Katz 2013-11-08 19:48:57 Re: How to script inserts where id is needed as fk
Previous Message George Woodring 2013-11-08 16:51:35 Query specific table using relative position in search path