﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22884	MySQL, unmanaged entity, does not get id after save()	Jossef Harush	nobody	"version
{{{(1, 6, 2, 'final', 0)}}}

i'm using a 3rd party mysql database and integrating with this table:

{{{
CREATE TABLE `alerts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` datetime NOT NULL,
  `message` text NOT NULL,
  `severity` int(11) NOT NULL,
  `state` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `type` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=943271 DEFAULT CHARSET=latin1;
}}}

iv'e used inspect db to generate this model:

{{{
class Alerts(models.Model):
    id = models.IntegerField(primary_key=True)
    date = models.DateTimeField()
    message = models.CharField(max_length=255)
    severity = models.IntegerField()
    state = models.IntegerField()
    title = models.CharField(max_length=255)
    type = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'alerts'

}}}

this is the snippet i'm using to create a new {{{alert}}} entity:

{{{
alert_entity = Alerts()

alert_entity.date = alert['date']
alert_entity.message = alert['message']
alert_entity.title = alert['title']
alert_entity.severity = alert['severity']
alert_entity.state = alert['state']
alert_entity.type = alert['type']

alert_entity.save()

}}}

after {{{.save()}}} i got {{{None}}} value in {{{.id}}} and in {{{.pk}}}

i tried things, and changing the model from

{{{
id = models.IntegerField(primary_key=True)
}}}

to:

{{{
id = models.AutoField(primary_key=True)
}}}

workaround my issue. (got the valid values of the new id in {{{.id}}} and in {{{.pk}}})

BTW, i'm getting these warning when saving:

{{{

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:903: RuntimeWarning: DateTimeField Tasks.end_date received a naive datetime (2014-06-18 22:15:13) while time zone support is active.
  RuntimeWarning)

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:903: RuntimeWarning: DateTimeField Tasks.start_date received a naive datetime (2014-06-18 22:05:13) while time zone support is active.
  RuntimeWarning)
}}}"	Uncategorized	closed	Database layer (models, ORM)	1.6	Normal	duplicate			Unreviewed	0	0	0	0	0	0
