SmallIntegerField
is smallint
(maximum signed value 32767
, display width 6
) in MySQL.
I suppose you want tinyint
(maximum signed value 127
, display width 3
) in MySQL.
class TinyAutoField(models.SmallAutoField):
def db_type(self, connection):
if connection.vendor == 'mysql':
return 'tinyint AUTO_INCREMENT'
return super().db_type(connection)
def rel_db_type(self, connection):
if connection.vendor == 'mysql':
return 'tinyint'
return super().db_type(connection)
Usage:
class tblroles(models.Model):
# role_id = SmallIntegerField(primary_key=True, verbose_name="role_id") # Change this
role_id = TinyAutoField(primary_key=True, verbose_name="role_id") # to this
# ...
But if you want literal int(3)
(maximum signed value 2147483647
, display width 3
) in MySQL.
class AutoField(models.AutoField):
def __init__(self, *args, **kwargs):
self.display_width = kwargs.pop('display_width', None)
super().__init__(*args, **kwargs)
def db_type(self, connection):
if connection.vendor == 'mysql' and self.display_width:
return 'int(%s) AUTO_INCREMENT' % self.display_width
return super().db_type(connection)
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
if self.display_width:
kwargs['display_width'] = self.display_width
return name, path, args, kwargs
Usage:
class tblroles(models.Model):
# role_id = SmallIntegerField(primary_key=True, verbose_name="role_id")
role_id = AutoField(primary_key=True, verbose_name="role_id", display_width=3)