Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big integer field in django models

Tags:

mysql

django

In short: How do you specify a BIGINT in Django models?

In a current project I am doing using Django (0.97-svn release), I've all the integer fields in all the models defined as IntegerField. It was working immaculately during the development cycle where I was using SQLite for the DB backend. However, as soon as I rolled off to MySQL, I noticed that for one of the IntegerFields, MySQL was truncating data---apparently, for reasons still unknown to me, SQLite didn't complain. I looked at the schema and found out that an IntegerField in Django is represented as an INT(11) field in MySQL. Naturally, the reason MySQL was truncating data was because the data was more than 11-digit in length. To workaround, I had to manually update the column to, in this case, be a BIGINT(20). Django and MySQL coexist peacefully with that. But whenever I reset the application in which rests the model containing that BIGINT, Django again creates it as an INT(11). I've looked at Django docs, and not found anything. Have I overlooked something?

Thanks.

like image 567
ayaz Avatar asked Nov 12 '08 11:11

ayaz


People also ask

Which Django model fields is used for integer value?

IntegerField is a integer number represented in Python by a int instance. This field is generally used to store integer numbers in the database. The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.

What is the max length of CharField in Django?

CharField since CharField has a max length of 255 characters while TextField can hold more than 255 characters.


2 Answers

BigIntegerField was added in changeset 11887, 2009-12-17 09:10:38 and is part of Django 1.2 and newer.

like image 153
kaleissin Avatar answered Oct 29 '22 22:10

kaleissin


SQLite won't complain ever. it uses 'manifest typing', that is, the values have type, not the columns. It lets you store bigtext on a smallint collumn, or whatever you want! (except if you define an integer primary key, where it uses a 64-bit integer).

that's a very convenient feature, but it makes a SQLite bad choice for developing if you're going to deploy with a different engine.

for using a BIGINT, you'd have to create a custom field class. unfortunately, that part has changed on Django 1.0, so you'd have to rewrite it if/when you update.

like image 22
Javier Avatar answered Oct 30 '22 00:10

Javier