How to reset the sequence for IDs on PostgreSQL tables

后端 未结 9 928
北荒
北荒 2020-12-05 02:37

I recently imported a lot of data from an old database into a new Postgresql database as the basis for models in a new Django site.

I used the IDs from the old datab

相关标签:
9条回答
  • 2020-12-05 03:23
    select setval('django_comments_id_seq', 12345);
    
    0 讨论(0)
  • 2020-12-05 03:30

    So the quickest, easiest and most "Django" way to do this in my opinion is to use the following management command:

    python manage.py sqlsequencereset app_name
    

    After this, you'll get something such as:

    BEGIN;
    SELECT setval(pg_get_serial_sequence('"measurements_quantity"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "measurements.Quantities";
    SELECT setval(pg_get_serial_sequence('"measurements.Prefixes"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "measurements.Prefixes";
    COMMIT;
    

    The next step is to run this in the python manage.py dbshell management command, so run this and then you'll see the interaction database shell in your terminal:

    psql (11.7 (Debian 11.7-0+deb10u1), server 11.5 (Debian 11.5-1.pgdg90+1))
    Type "help" for help.
    
    postgres=# BEGIN;
    BEGIN
    postgres=# SELECT setval(pg_get_serial_sequence('"measurements.Quantities"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "measurements.Quantities";
     setval 
    --------
          1
    (1 row)
    
    postgres=# SELECT setval(pg_get_serial_sequence('"measurements.Prefixes"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "measurements.Prefixes";
     setval 
    --------
          1
    (1 row)
    
    postgres=# COMMIT;
    COMMIT
    postgres=# exit
    

    Simple as that. The python manage.py sqlsequencereset app_name command will give you the SQL you need to run, and you run it in the dbshell.

    No writing your own custom SQL or custom code and it will give you what you need in the correct format and db engine of choice.

    0 讨论(0)
  • 2020-12-05 03:31

    Here's a short snippet to reset all sequences in Django 1.9+ (based on http://djangosnippets.org/snippets/2774/) and compatible with Python 3:

    import os
    from io import StringIO
    
    os.environ['DJANGO_COLORS'] = 'nocolor'
    
    from django.core.management import call_command
    from django.apps import apps
    from django.db import connection
    
    commands = StringIO()
    cursor = connection.cursor()
    
    for app in apps.get_app_configs():
        label = app.label
        call_command('sqlsequencereset', label, stdout=commands)
    
    cursor.execute(commands.getvalue())
    
    0 讨论(0)
提交回复
热议问题