Can a primary key use BigInteger as an AutoField in Django 1.2.4?

The default primary key seems to be int. Do I need to use a large integer for autofield as a primary key?

+4
source share
8 answers

There are several ways I can implement this. In any case, you must define your pk field.

First of all, just create your own id field and override the save method.

modelname(models.Model): # model definition def save(self): self.pkfield = nextIntFucntion() super(modelname, self).save() 

nextIntFunction() quite simple with requesting objects ordered by id and then getting id + 1

I also found this link of BigIntegerField and BigAutoField , which seems to solve the problem, but I have not tested it myself.

+2
source

I met the same question. I added code like

 User._meta.has_auto_field = True User._meta.auto_field = id 

And I define the id field for BigIntegerField (primary_key = True) After I use user.Save (), user.id will have its identifier, I no longer need a request. I think this works, but it is not a beautiful solution, so I still find a good way.

+2
source

I suggest you use the new Django. Django's official documentation now goes no further than 1.3. And 1.3 is unsafe and not supported. I understand that the question was asked more than 3 years ago, but since there is still no answer, I will give him a chance.

In Django 1.6.5, you can simply do this in your model:

 class MyModel(models.Model): id = models.BigIntegerField(unique=True, primary_key=True) 

primary_key=True override the default identifier for the model. When used, this field automatically increases with each new model object. It just works!

+2
source

Since Django 1.10 , you can use BigAutoField , as described in the documentation, it works just like AutoField , but it will be guaranteed to correspond to numbers from 1 to 9223372036854775807.

So you can use it like:

 class SomeModel(models.Model): id = models.BigAutoField() ... 
+2
source

http://docs.djangoproject.com/en/dev/topics/db/models/

class BigIntegerField ([** options])

Available option:

primary_key If True, this field is the primary key for the model.

And you are doing the southern migration: ALTER TABLE mytable CHANGE COLUMN myid BIGINT (20) NOT NULL AUTO_INCREMENT;

+1
source

You can hack Django and change the default default keys to the correct values. Departure:

http://code.djangoproject.com/browser/django/trunk/django/db/backends/mysql/creation.py

 from django.conf import settings from django.db.backends.creation import BaseDatabaseCreation class DatabaseCreation(BaseDatabaseCreation): # This dictionary maps Field objects to their associated MySQL column # types, as strings. Column-type strings can contain format strings; they'll # be interpolated against the values of Field.__dict__ before being output. # If a column type is set to None, it won't be included in the output. data_types = { 'AutoField': 'integer AUTO_INCREMENT', 'BooleanField': 'bool', 'CharField': 'varchar(%(max_length)s)', 

You can change this using the patch in your own code:

 DatabaseCreation.data_types['AutoField'] = 'bigint AUTO_INCREMENT' 

You will also have to fix the AutoField class:

 http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/__init__.py 

(untested code, luck)

+1
source

You are right, sorry. The required fragment is here:

http://djangosnippets.org/snippets/1244/

Allows you to create the fields biginter (mysql), bigserial (psql) or NUMBER (19) (oracle), which have auto-increment specified with AutoField django, therefore ensuring that the identifier is updated in the instance when its save () method is called.

If you only subclass IntegerField into BigIntegerField and use it as your primary key, your model instance that you create will not get the id attribute specified when you call "save ()", instead you will have to query and load the instance from the database again to get the id.

0
source

These fragments work. Use the BigAutoField class as the primary key on your model, and it works without any hacking.

0
source

Source: https://habr.com/ru/post/1337570/


All Articles