From: | Richard Huxton <dev(at)archonet(dot)com> |
---|---|
To: | Milos Prudek <prudek(at)bvx(dot)cz> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: serial autoincrement and related table |
Date: | 2004-05-17 11:56:02 |
Message-ID: | 40A8A852.90309@archonet.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Milos Prudek wrote:
> I have a serial autoincrement column called "idmember" in my main table
> (members). This serial column is a key to a second table. A row in
> "members" table corresponds to many rows in the second table.
> My question is: is this the best practice?
>
> Here's an example in Python:
> conn=psycopg.connect(dbconnstr)
> c=conn.cursor()
> # LOOP BEGINS HERE...
> Cmd = "INSERT INTO members ... VALUES (...);"
> c.execute(Cmd, Data)
> Cmd = "SELECT currval('members_idmember_seq') FROM members LIMIT 1;"
A simple "SELECT currval('members_idmember_seq');" will do it. The
sequence isn't part of the table.
> c.execute(Cmd)
> idmember = c.fetchone()[0]
> Cmd = "INSERT INTO msg (idmember,txt) VALUES (%s,%s);"
Alternatively, you could rewrite this query:
"INSERT INO msg (idmember,txt) VALUES (currval('members_idmember_seq'),
%s);"
--
Richard Huxton
Archonet Ltd
From | Date | Subject | |
---|---|---|---|
Next Message | Milos Prudek | 2004-05-17 12:03:39 | Re: serial autoincrement and related table |
Previous Message | Milos Prudek | 2004-05-17 11:52:55 | Re: serial autoincrement and related table |