django id integer limit
Asked Answered
W

3

14

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.

Warpath answered 27/8, 2012 at 23:36 Comment(4)
The limit is dependent on postgresql's maximum value for an integer fieldTarge
AFAIK, it is limited only by the backend database engine. In the case of postgres, I doubt you will ever reach the 2,147,483,647 limit.Delwin
@PauloScardine: And there's always bigserial (9 223 372 036 854 775 807) if needed.Outcome
Just an FYI - using two different database backends between local development and production may cause issues/inconsistencies etc. It "should" be fine for simple projects, but just remember you should replicate your production environment as closely as possible whilst in development. (Also, PostgreSQL backends allow for slightly different field types as well: docs.djangoproject.com/en/2.2/ref/contrib/postgres/fields)Yesima
S
11

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.

Sandal answered 7/4, 2013 at 2:58 Comment(0)
T
11

Adding new info for Django 2.x:


Djangos AutoField is an IntegerField. The docs for IntegerField say:

Values from -2.147.483.648 to 2.147.483.647 are safe in all databases supported by Django.

AutoField per default only uses positive numbers, so the values can go from 1 to 2.147.483.647.


Since Django 1.10 there is also an BigAutoField, which is very similar to the BigIntegerField. The docs for BigAutoField say:

A 64-bit integer [...] that is guaranteed to fit numbers from 1 to 9.223.372.036.854.775.807.

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.

Tipple answered 7/7, 2018 at 12:41 Comment(0)
J
1

As others have mentioned whether you use int or bigint, respectively you have 2.147.483.648 or 9.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)

Joaquinajoash answered 15/8, 2018 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.