Created
July 29, 2014 12:03
-
-
Save balazs-endresz/c4d5b342d0cd4c468a6d to your computer and use it in GitHub Desktop.
Update the ID sequence for a given model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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