I have a very basic model in Django:
class Case(models.Model):name = models.CharField(max_length=255)created_at = models.DateTimeField(default=datetime.now)updated_at = models.DateTimeField(default=datetime.now)def save(self):if self.created_at == None:self.created_at = datetime.now()self.updated_at = datetime.now()super(Case, self).save()class Meta:db_table = u'cases'
Because I did not specify the PK, Django took care of that for me. I see the field in my database which is called "id", marked as a primary key and auto-increment. I find odd to be getting that warning since everything is cool on the DB and model end. The error is:
_mysql_exceptions.WarningWarning: Field 'id' doesn't have a default value
My work around when saving is to set the id to 0. It kills the warning and the id gets set properly anyway since MySQL handles it.
case = Case()
case.id = 0 #ugly workaround for - Warning: Field 'id' doesn't have a default value
case.name = request.POST[u'case[name]']
case.save()
The issue with this solution is:
- It's ugly
- The new PK is inaccessible after saving which makes it impossible to serialize properly
Does anyone know how to fix this?
I'm running:
Python 2.7.2
django.VERSION (1, 3, 1, 'final', 0)
mysql Ver 14.14 Distrib 5.1.45, for apple-darwin10.2.0 (i386) using readline 5.1
MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg
And my create table looks like this:
CREATE TABLE `cases` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) NOT NULL,`created_at` datetime NOT NULL,`updated_at` datetime NOT NULL,PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=latin1