[PYTHON] Set BigIntegerField to auto increment

I'll forget it, so make a note for myself. There is AutoField in Django, but the clumn of the database created by it will be int.

I want to auto increment with bigint. There was a kind person on stackoverflow.

from django.db.models import fields
from south.modelsinspector import add_introspection_rules

class BigAutoField(fields.AutoField):
    def db_type(self, connection):
        if 'mysql' in connection.__class__.__module__:
            return 'bigint AUTO_INCREMENT'
        return super(BigAutoField, self).db_type(connection)

add_introspection_rules([], ["^MYAPP\.fields\.BigAutoField"])

For those who do not use south

from django.db.models import fields

class BigAutoField(fields.AutoField):
    def db_type(self, connection):
        if 'mysql' in connection.__class__.__module__:
            return 'bigint AUTO_INCREMENT'
        return super(BigAutoField, self).db_type(connection)


This seems to be fine.

Experiment

First, prepare a test model One thing to note when using AutoIncrement is if it has been used in another field.

A model can't have more than one AutoField.

I get angry.

from django.db import models
class test(models.Model):
	id = BigAutoField(primary_key=True)
python manage.py syncdb

result

スクリーンショット 2015-09-13 16.58.14.png

It became auto_increment!

If you have any supplements or mistakes, please point them out.

reference: http://stackoverflow.com/questions/2672975/django-biginteger-auto-increment-field-as-primary-key

Recommended Posts

Set BigIntegerField to auto increment
Set VS Code to PyCharm.
Set opset to embed in ONNX