Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django reached maximum value of sequence

I am running a program that creates millions of records daily, after running this program for over a year it seems like I have reached the id limit on my postgres database.

The error I am receiving is

django.db.utils.DataError: nextval: reached maximum value of sequence "main_records_id_seq" (2147483647)

Is there a way to extend the maximum id using SQL or the django ORM? Or is there a better solution?

like image 727
cobysh Avatar asked Dec 06 '22 08:12

cobysh


1 Answers

Django uses by default an AutoField [Django-doc] to specify values for the primary key at the database side. An AutoField is an IntegerField [Django-doc], and as the Django documentation says:

An integer. Values from -2147483648 to 2147483647 are safe in all databases supported by Django.

What you can use instead is a BigAutoField [Django-doc], which will use:

A 64-bit integer, much like an AutoField except that it is guaranteed to fit numbers from 1 to 9223372036854775807.

If your application generates 2'147'483'647 in one year, you can make use of a BigAutoField for 4'294'967'298 years.

You thus can define your model as:

class MyModel(models.Model):
    id = models.BigAutoField(primary_key=True)
like image 166
Willem Van Onsem Avatar answered Jan 02 '23 19:01

Willem Van Onsem