Opened 13 years ago

Closed 9 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

Description

Hi,

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

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

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)

Cheers,
Michael

Attachments (1)

custommodels.py (1.9 KB ) - added by michael.frere@… 13 years ago.
custom model fields tryout

Download all attachments as: .zip

Change History (8)

by michael.frere@…, 13 years ago

Attachment: custommodels.py added

custom model fields tryout

comment:1 by Anssi Kääriäinen, 13 years ago

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 by anonymous, 12 years ago

Type: New featureBug
Version: 1.31.4

comment:3 by mmmnow@…, 12 years ago

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,
                                       PositiveSmallIntegerField)))):
            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 by mmmnow@…, 12 years ago

Resolution: duplicate
Status: closednew

comment:5 by anonymous, 12 years ago

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

comment:6 by anonymous, 12 years ago

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 by Tim Graham, 9 years ago

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