Is there a limit to the AutoField in a Django model or the database back-ends?
The Django project I am working on could potentially see a lot of objects in certain database tables which would be in excess of 40000 within a short amount of time.
I am using Sqlite for dev and Postgresql for production.
Adding this as an answer. Django maps this to serial columns which means that the maximum value is in the 2 billion range ( 2,147,483,647 to be exact). While that is unlikely to be an issue for most applications, if you do, you could alter the type to become a bigint instead and this would make it highly unlikely you will ever reach the end of 64-bit int space.
As others have mentioned whether you use
int
orbigint
, respectively you have2.147.483.648
or9.223.372.036.854.775.808
values.If you think to exceed those numbers, you can do sharding. In short, sharding is a way of horizontally partitioning your data by storing different rows of the same table in multiple tables across multiple databases.
There are available many sharding libraries for Django (e.g. django-sharding)
Adding new info for
Django 2.x
:Djangos
AutoField
is anIntegerField
. The docs forIntegerField
say:AutoField
per default only uses positive numbers, so the values can go from1
to2.147.483.647
.Since
Django 1.10
there is also anBigAutoField
, which is very similar to theBigIntegerField
. The docs forBigAutoField
say:Now, the docs don't say it explicitly for this field, but by using the words
is garanteed to fit
, I assume that applies for all databases supported by Django.