Opened 8 years ago

Closed 5 years ago

#18201 closed Bug (duplicate)

smallint and tinyint fields + auto increment

Reported by: michael.frere@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



I tried to create custom model fields, taking inspiration from the following Django snippets:

I managed to get standard TINYINT fields and I can make them UNSIGNED aswell (using MySQL)
However when I try to make a custom model from an AutoField and make it a primary key, referenced in other tables as a foreign key. Django keeps using INT(11) for the foreign keys upon South migration instead of the expected TINYINT (tried with SMALLINT as well).

Could you help me to get this working or maybe implement some sort of Tiny/Small/BigAutoField in some future Django release? I'll attach my current code (python script)


Attachments (1) (1.9 KB) - added by michael.frere@… 8 years ago.
custom model fields tryout

Download all attachments as: .zip

Change History (8)

Changed 8 years ago by michael.frere@…

Attachment: added

custom model fields tryout

comment:1 Changed 8 years ago by Anssi Kääriäinen

Resolution: duplicate
Status: newclosed

I am not sure what this ticket is about - custom AutoField types, a problem with South or just help request for implementing custom fields in user code. I think django-users is the correct forum for now.

#14286 is about adding bigint based AutoField, and this ticket's request for smallint based AutoFields could be handled in that ticket, too. I am resolving this as duplicate of that ticket, and adding a comment there about the possibility to support smallint, too. As Django doesn't have a tinyint field it is likely there will not be tinyint based AutoField either.

comment:2 Changed 7 years ago by anonymous

Type: New featureBug
Version: 1.31.4

comment:3 Changed 7 years ago by mmmnow@…

Sorry, but this ticket is not a duplicate and orginal author of this ticket is right.

It would be great to be able to define custom fields based on AutoField. The problem is in ForeignKey's db_type method:

    def db_type(self, connection): 
        rel_field = self.rel.get_related_field()
        if (isinstance(rel_field, AutoField) or
                (not connection.features.related_fields_match_type and
                isinstance(rel_field, (PositiveIntegerField,
            return IntegerField().db_type(connection=connection)
        return rel_field.db_type(connection=connection)

As you see, if ForeignKey is pointing to instance of AutoKey then ForeignKey ignores db_type mythod of related field, which is wrong and buggy. The simple fix would be to change this method to:

    def db_type(self, connection): 
        rel_field = self.rel.get_related_field()
        return rel_field.db_type(connection=connection)

comment:4 Changed 7 years ago by mmmnow@…

Resolution: duplicate
Status: closednew

comment:5 Changed 7 years ago by anonymous

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted

comment:6 Changed 7 years ago by anonymous

I'm not sure the proposed fix would work as you don't always want the foreign key field to have the exact same definition as it's related field. But it does seem like there should be a way for the target field to specify what db types foreign keys pointing to it should have.

comment:7 Changed 5 years ago by Tim Graham

Resolution: duplicate
Status: newclosed

I believe this is a duplicate of #13774 -- Add model Field.rel_db_type() method.

Note: See TracTickets for help on using tickets.
Back to Top