Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How big can Django primary keys get?

Is there a maximum value as to how high pk values for a model can get? For example, for something like an activity feed model, the pk's can get really large.

Ex, "Kyle liked this post", "Alex commented on this post" etc..As you can see, for every action, an activity feed object is created. Is there some sort of maximum threshold that will be reached? I've done some research but haven't found a concise answer. If there is some limit, how can one overcome this?

I currently use PostgreSQL as my database.

like image 780
deadlock Avatar asked Nov 19 '13 20:11

deadlock


People also ask

Does every Django model have a primary key?

Every table should have a primary key, so every model should have a primary key field. However, you do not have to do this manually if you do not want. By default, Django adds an id field to each model, which is used as the primary key for that model.

What is a primary key in Django?

primary_key: If True , sets the current field as the primary key for the model (A primary key is a special database column designated to uniquely identify all the different table records). If no field is specified as the primary key, Django will automatically add a field for this purpose.

Does Django support multiple column primary keys?

Do Django models support multiple-column primary keys? ¶ No. Only single-column primary keys are supported.

How do I set primary key in Django?

If you'd like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you've explicitly set Field.primary_key , it won't add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).


1 Answers

Django's auto-incrementing IDs are AutoFields, a subclass of IntegerField. It will generate the primary keys as PostgreSQL integer, which are signed 32-bit integers with a maximum value of 2147483647, a little over 2 billion.

like image 63
RemcoGerlich Avatar answered Oct 15 '22 14:10

RemcoGerlich