Skip to content

Instantly share code, notes, and snippets.

@balazs-endresz
Created July 29, 2014 12:03
Show Gist options
  • Save balazs-endresz/c4d5b342d0cd4c468a6d to your computer and use it in GitHub Desktop.
Save balazs-endresz/c4d5b342d0cd4c468a6d to your computer and use it in GitHub Desktop.
Update the ID sequence for a given model
def fix_id_sequence(model_class):
from django.db import connection
next_val = model_class.objects.all().order_by("-id")[0].id + 1
cursor = connection.cursor()
cursor.execute("select setval('%s_id_seq', %d, True)" % (model_class._meta.db_table, next_val))
row = cursor.fetchone()
cursor.close()
return row[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment