On Mon, May 19, 2008 at 01:20:30AM -0700, Sam's Lists wrote:
> Is there some way to get postgres to figure out what to set the next value
> for each table?
I don't know any magic. Sequences are supposed to be automatically
handled in case the user doesn't fiddle with serial fields. When one does
DB dump (using pg_dump utility) Postgres inserts necessary commands to
reset sequences on load. But if one loads data (whether with INSERT command
or with COPY) and the data touches serial fields, [s]he must handle
sequences by hand.
> I have 35 tables so doing it by hand doesn't seem like fun.
> I guess I could write something in Python, but that seems like a pain too.
[pseudocode]
for table in tables:
sequnce_name = "%s_id_seq" % table
query = "SELECT MAX(id) FROM %s;" % table
id = exec(query)
query = "SELECT setval(%s, %d);" % (sequnce_name, id)
exec(query)
It is enough to use id, not id+1 because after setval() next time you
execute an INSERT the sequence will be automatically increased before
INSERT. The other options is
query = "SELECT setval(%s, %d, 0);" % (sequnce_name, id+1)
In this case on next INSERT the sequence will only be increased *after* the
INSERT, so we set id+1 in advance.
Oleg.
--
Oleg Broytmann http://phd.pp.ru/ [EMAIL PROTECTED]
Programmers don't die, they just GOSUB without RETURN.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss