views:

1394

answers:

3

I have been working on an application in Django. To begin with, for simplicity, I had been using sqlite3 for the database.

However, once I moved to PostgreSQL, I've run into a bit of a problem: the primary key does not reset once I clear out a table.

This app is a game that is played over a long time period (weeks). As such, every time a new game starts, all of the data is cleared out of the database and then new, randomized data is added.

I'd like to be able to "start over" with primary keys starting at 1 each time I clean/rebuild the game.

The code still works as-is, but integers are a pretty natural way for describing the objects in my game. I'd like to have each new game start at 1 rather than wherever the last game left off.

How can I reset the primary key counter in PostgreSQL? Keep in mind that I don't need to preserve the data in the table since I am wiping it out anyway.

A: 

You need to truncate the table. See http://www.postgresql.org/docs/8.1/static/sql-truncate.html

superUntitled
Doesn't seem to work... deletes all the data, but the primary keys still start where they left off before. For example, if I insert 500 rows, then truncate the table, then insert another 500, the primary keys will be from 501 to 1000, not from 1-500.
TM
Hmmm, try this: ALTER TABLE theTableInQuestion AUTO_INCREMENT=0
superUntitled
Never mind, that is for mySql, sorry I cannot not be of any assistance.
superUntitled
+6  A: 

In your app directory try this:

python manage.py help sqlsequencereset

Pipe it into psql like this to actually run the reset:

python manage.py sqlsequencereset myapp1 myapp2 | psql

Edit: here's an example of the output from this command on one of my tables:

BEGIN;
SELECT setval('"project_row_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "project_row";
COMMIT;
Van Gale
what if its a windows setup?
fuentesjr
The sqlsequencereset commmand generates the SQL needed to reset the primary key counter, so on windows, instead of piping the output to psql, I would copy+paste it into whatever admin app you use. For example, if pgAdminIII is installed, paste into "execute arbitrary SQL queries".
Van Gale
I will check this out when I get home from work, thanks! Is there way to do this programmatically? I'd like to be able to easily stick it in a django view if possible.
TM
That seems a bit dangerous, but to each his own! Use this method: http://docs.djangoproject.com/en/dev/topics/db/sql/
Van Gale
Yes, I know it seems dangerous, but I'd like people using this game to be able to restart it without having to know the table structure or django commands. Really the only data "at risk" is randomized game state data for their own install of the game.
TM
+1  A: 

I view auto-increment primary keys as purely internal identifiers for database records, and I don't like exposing them to users. Granted, it's a common design to use them as part of URLs, but even there slugs or other identifiers feel more appropriate.

akaihola
I normally would too, but the objects in this case are already going to be numbered, and displayed as 1, 2, 3... etc.
TM