Subclassing a model with a custom AutoField

43 views
Skip to first unread message

jeroenp

unread,
Oct 1, 2013, 6:30:49 AM10/1/13
to django...@googlegroups.com
Hi,

I'm trying to save space in database rows in tables that have bigint-sized numbers of rows. One of the things I'm trying to do is using a smallint for the `id` on one of the models that is pointed to from those big tables:

I have a model A with a custom AutoField for its primary key that uses a smallint instead of int by way of a custom db_type method. I have a second model B that subclasses A.  Model C is the model with the large number of rows and has a foreign key to B. Django generates an int-sized column in model B for the foreign key to the superclass A. I hoped for a smallint-sized column, as that's what the related field is using. MySQL spits out an error.

I'm reading the ForeignKey.db_type method (rel 1.6): I don't quite understand why it insists on returning the db_type from IntegerField when my field is an instance of AutoField. I'm guessing it has something to do with AutoField's db_type returning AUTO_INCREMENT along with its column type. This works if the field is specifically of the builtin type AutoField, but not necessarily with some random subclass;

And as I understand it from the __new__ in ModelBase I invariably end up with a OneToOneField for the `a_ptr_id` field in the subclass. So I don't seem to be able to "patch" that field, like I can with the `id` field in model A or a regular foreign key.

All I can come up with is this yucky:

>>> B._meta.get_field('a_ptr').db_type = lambda connection: 'smallint'

..which works, or I could create a standalone model B that "inherits" from model A without using Python inheritance, i.e. explicitly create a smallint primary key and fiddle with create statements so that I also get records in A. 

Is there any (nice) way I can persuade Django to return a different db_type for that OneToOneField in the subclass?

thanks,
Jeroen Pulles
Reply all
Reply to author
Forward
0 new messages