Django重置用于生产的自动递增pk / id字段 [英] Django reset auto-increment pk/id field for production

查看:555
本文介绍了Django重置用于生产的自动递增pk / id字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我是Django,Python和Postgresql的新手)我一直在开发过程中添加和删除数据,并注意到pk会不断增加,即使删除所有模型也永远不会重置为1。在我将其推送到生产环境之前,是否可以将pk重置为从1开始?

(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?

推荐答案

您可以使用 sqlsequencereset重置模型ID序列 code>命令

You can reset model id sequence using sqlsequencereset command

python manage.py sqlsequencereset myapp1 myapp2 myapp3| psql

如果要读取生成的sql命令,只需执行该命令,而无需将其通过管道传递到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

您需要在生产数据库上使用此命令。但是,正如@knbk所述,如果您的生产数据库是新数据库,则无需重置ID序列。

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.

这篇关于Django重置用于生产的自动递增pk / id字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆