I've been focusing on a credit card applicatoin in Django. To start with, for simplicity, I used to be using sqlite3 for that database.

However, after i gone to live in PostgreSQL, I have encounter a little of the problem: the main key doesn't totally reset after i obvious out a table.

This application is a game title that's performed on the very long time period (days). As a result, whenever a new game begins, all the information is removed from the database after which new, randomized information is added.

Let me have the ability to "begin again" with primary secrets beginning at 1 every time I clean/rebuild the overall game.

The code still works as-is, but integers really are a pretty natural method for explaining the objects during my game. Let me have each new game start at 1 instead of wherever the final game left off.

How do i totally reset the main key counter in PostgreSQL? Bear in mind which i don't have to preserve the information within the table since i have am wiping it anyway.

Inside your application directory do this:

python manage.py help sqlsequencereset

Pipe it into psql such as this to really run the totally reset:

python manage.py sqlsequencereset myapp1 myapp2 | psql

Edit: here's a good example of the output out of this command on a single of my tables:

BEGIN;
SELECT setval('"project_row_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "project_row";
COMMIT;

I view auto-increment primary secrets as purely internal identifiers for database records, and that i can't stand subjecting these to customers. Granted, it is a common design for their services included in Web addresses, but even there slugs or any other identifiers feel appropriate.

Should you execute a raw sql, can perform this:

ALTER SEQUENCE youApp_id_seq RESTART WITH 1;

paperwork: http://www.postgresql.org/docs/8.2/static/sql-altersequence.html