django id integer limit

被刻印的时光 ゝ 提交于 2019-11-28 07:14:51

问题


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.


回答1:


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.




回答2:


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.




回答3:


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)



来源:https://stackoverflow.com/questions/12150973/django-id-integer-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!