Django重置自动增量pk / id字段用于生产

时间:2015-09-11 20:30:32

标签: python django postgresql django-models

(我是Django,Python和Postgresql的新手)我一直在开发中添加和删除数据,并注意到即使我删除了所有模型,pk仍然会加起来并且永远不会重置为1。在将其推送到生产之前,是否可以将pk重置为从1开始?这样做是个好主意吗?

3 个答案:

答案 0 :(得分:7)

您可以使用python manage.py sqlsequencereset myapp1 myapp2 myapp3| psql 命令重置模型ID序列

python manage.py sqlsequencereset myapp1 myapp2 myapp3

如果你想读取生成的sql命令,只需执行该命令而不管道它到psql。

{{1}}

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

答案 1 :(得分:2)

sqlsequencereset 的替代方法:直接用 SQLite 更新

使用默认 db.sqlite3 数据库的开发环境

我在尝试这里给出的答案时挣扎了一段时间,但我不断收到:

python manage.py sqlsequencereset AppName
>> No sequences found.

对我来说最简单的解决方法是直接更新我的 SQLite 数据库(我在本地运行我的应用程序):

# Open your database
sqlite3 db.sqlite3

并且,在 SQLite 提示中:

UPDATE sqlite_sequence SET seq = 0 WHERE sqlite_sequence.name = "<AppName_ModelName>";

我将值设置为零,所以它从 id = 1 开始。

编辑:这是我的第一篇文章,如果我应该改进格式,请告诉我!

答案 2 :(得分:0)

您可以生成一个命令,该命令收集系统中的所有应用程序,并为每个表运行动态生成的reset语句,同时还可以动态收集循环的每个表的PK列名(如果未命名)它们都具有相同的值。

要运行:python manage.py reset_sequences

import psycopg2
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connections


def dictfetchall(cursor):
    """Return all rows from a cursor as a dict"""
    columns = [col[0] for col in cursor.description]
    return [
        dict(zip(columns, row))
        for row in cursor.fetchall()
    ]


class Command(BaseCommand):
    help = "Resets sequencing errors in Postgres which normally occur due to importing/restoring a DB"

    def handle(self, *args, **options):
        # loop over all databases in system to figure out the tables that need to be reset
        for name_to_use_for_connection, connection_settings in settings.DATABASES.items():
            db_name = connection_settings['NAME']
            host = connection_settings['HOST']
            user = connection_settings['USER']
            port = connection_settings['PORT']
            password = connection_settings['PASSWORD']

            # connect to this specific DB
            conn_str = f"host={host} port={port} user={user} password={password}"

            conn = psycopg2.connect(conn_str)
            conn.autocommit = True

            select_all_table_statement = f"""SELECT *
                                    FROM information_schema.tables
                                    WHERE table_schema = 'public'
                                    ORDER BY table_name;
                                """
            # just a visual representation of where we are
            print('-' * 20, db_name)
            try:
                not_reset_tables = list()
                # use the specific name for the DB
                with connections[name_to_use_for_connection].cursor() as cursor:
                    # using the current db as the cursor connection
                    cursor.execute(select_all_table_statement)
                    rows = dictfetchall(cursor)
                    # will loop over table names in the connected DB
                    for row in rows:
                        find_pk_statement = f"""
                            SELECT k.COLUMN_NAME
                            FROM information_schema.table_constraints t
                            LEFT JOIN information_schema.key_column_usage k
                            USING(constraint_name,table_schema,table_name)
                            WHERE t.constraint_type='PRIMARY KEY'
                                AND t.table_name='{row['table_name']}';
                        """
                        cursor.execute(find_pk_statement)
                        pk_column_names = dictfetchall(cursor)
                        for pk_dict in pk_column_names:
                            column_name = pk_dict['column_name']

                        # time to build the reset sequence command for each table
                        # taken from django: https://docs.djangoproject.com/en/3.0/ref/django-admin/#sqlsequencereset
                        # example: SELECT setval(pg_get_serial_sequence('"[TABLE]"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "[TABLE]";
                        try:
                            reset_statement = f"""SELECT setval(pg_get_serial_sequence('"{row['table_name']}"','{column_name}'), 
                                                    coalesce(max("{column_name}"), 1), max("{column_name}") IS NOT null) FROM "{row['table_name']}" """
                            cursor.execute(reset_statement)
                            return_values = dictfetchall(cursor)
                            # will be 1 row
                            for value in return_values:
                                print(f"Sequence reset to {value['setval']} for {row['table_name']}")
                        except Exception as ex:
                            # will only fail if PK is not an integer...
                            # currently in my system this is from django.contrib.sessions
                            not_reset_tables.append(f"{row['table_name']} not reset")

            except psycopg2.Error as ex:
                raise SystemExit(f'Error: {ex}')

            conn.close()
            print('-' * 5, ' ALL ERRORS ', '-' * 5)
            for item_statement in not_reset_tables:
                # shows which tables produced errors, so far I have only
                # seen this with PK's that are not integers because of the MAX() method
                print(item_statement)

            # just a visual representation of where we are
            print('-' * 20, db_name)



相关问题