"INT out of range" - How to Reset ID (Primary Key) in a Django Model

The "INT out of range" error in Django usually occurs when a field in your model has an integer type with a limited range (such as IntegerField) but you are trying to insert or update a value that exceeds the allowed range for that field.
 

 

How to Reset ID (Primary Key) in a Django Model

If you want to reset the id or primary key (pk) of a Django model, you'll need to consider how to handle database tables and how to reset auto-increment fields. Below are a few different methods:

Option 1: Reset ID using Database Command (PostgreSQL/MySQL)

The primary key in a model increments automatically. To reset it, you need to reset the auto-increment sequence of the table. Here are the commands for various databases:

PostgreSQL

ALTER SEQUENCE yourapp_modelname_id_seq RESTART WITH 1;

You can run this command via Django’s dbshell:

python manage.py dbshell

Then run the SQL command above, replacing yourapp_modelname_id_seq with the actual sequence name (usually composed of the app name, model name, and _id_seq).

MySQL

ALTER TABLE yourapp_modelname AUTO_INCREMENT = 1;

Option 2: Use SQL with cursor() in a Migration or Django Shell

You can also execute raw SQL through Django’s cursor():

from django.db import connection

with connection.cursor() as cursor:
    cursor.execute("ALTER SEQUENCE yourapp_modelname_id_seq RESTART WITH 1;")

Option 3: Truncate the Table

If you don’t need to preserve any of the existing records, you can truncate the table. This will delete all records and reset the primary key to 1:

from yourapp.models import ModelName

ModelName.objects.all().delete()

After deleting all records, you may need to reset the sequence manually as shown above. Alternatively, you could truncate the table with a raw SQL command:

from django.db import connection

with connection.cursor() as cursor:
    cursor.execute("TRUNCATE TABLE yourapp_modelname RESTART IDENTITY CASCADE;")

This will delete all rows and reset the ID sequence to 1.

Option 4: Custom Migration

If you're working in a development environment and want to do this through Django's migrations system, you can create a custom migration:

  1. Create an empty migration file:
  2. python manage.py makemigrations --empty yourappname
  3. Open the migration file and add the code to reset the sequence:
  4. from django.db import migrations, connection
    
    def reset_model_id_sequence(apps, schema_editor):
        with connection.cursor() as cursor:
            cursor.execute("ALTER SEQUENCE yourapp_modelname_id_seq RESTART WITH 1;")
    
    class Migration(migrations.Migration):
    
        dependencies = [
            ('yourappname', 'previous_migration_file'),
        ]
    
        operations = [
            migrations.RunPython(reset_model_id_sequence),
        ]

Caution

  • Production Environment: Be cautious when modifying primary keys in a production database. Changing primary key values can lead to loss of data integrity, especially if other tables have foreign key relationships.
  • Data Integrity: Ensure you don't have foreign key constraints that might be violated when resetting IDs.

Conclusion

There are different approaches you can use to reset the id primary key of a model, and the method you choose depends on your use case. The typical way involves either resetting the auto-increment sequence with an SQL command or truncating the entire table.

Comments