问题
(I'm new to Django, Python, and Postgresql) I've been adding and deleting data in my development and noticed that the pk keeps adding up and never reset to 1 even if I delete all the models. Is it possible to reset the pk to start from 1 before I push this up to the production? Is it a good idea to do that?
回答1:
You can reset model id sequence using sqlsequencereset
command
python manage.py sqlsequencereset myapp1 myapp2 myapp3| psql
If you want to read the generated sql command, just execute that command without pipe it to psql.
python manage.py sqlsequencereset myapp1 myapp2 myapp3
You need use this command over your production database. But, as @knbk mentioned, if your production database is new, you don't need to reset id sequences.
来源:https://stackoverflow.com/questions/32531677/django-reset-auto-increment-pk-id-field-for-production