django id integer limit

2019-01-27 22:34发布

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.

3条回答
家丑人穷心不美
2楼-- · 2019-01-27 22:36

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.

查看更多
Melony?
3楼-- · 2019-01-27 22:39

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)

查看更多
对你真心纯属浪费
4楼-- · 2019-01-27 22:41

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.

查看更多
登录 后发表回答