How to get Django AutoFields to start at a higher number

前端 未结 7 693
挽巷
挽巷 2020-12-02 19:15

For our Django App, we\'d like to get an AutoField to start at a number other than 1. There doesn\'t seem to be an obvious way to do this. Any ideas?

7条回答
  •  伪装坚强ぢ
    2020-12-02 19:59

    Here is what I did..

    def update_auto_increment(value=5000, app_label="remrate_data"):
        """Update our increments"""
        from django.db import connection, transaction, router
        models = [m for m in get_models() if m._meta.app_label == app_label]
        cursor = connection.cursor()
        for model in models:
            _router = settings.DATABASES[router.db_for_write(model)]['NAME']
            alter_str = "ALTER table {}.{} AUTO_INCREMENT={}".format(
                _router, model._meta.db_table, value)
            cursor.execute(alter_str)
            transaction.commit_unless_managed()
    

提交回复
热议问题