Code

Opened 2 years ago

Last modified 15 months ago

#18201 new Bug

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@… 2 years ago.
custom model fields tryout

Download all attachments as: .zip

Change History (7)

Changed 2 years ago by michael.frere@…

custom model fields tryout

comment:1 Changed 2 years ago by akaariai

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

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 15 months ago by anonymous

  • Type changed from New feature to Bug
  • Version changed from 1.3 to 1.4

comment:3 Changed 15 months 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,
                                       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 Changed 15 months ago by mmmnow@…

  • Resolution duplicate deleted
  • Status changed from closed to new

comment:5 Changed 15 months ago by anonymous

  • Component changed from Uncategorized to Database layer (models, ORM)
  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 15 months 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.