#22884: MySQL, unmanaged entity, does not get id after save()
----------------------------------------------+--------------------
     Reporter:  Jossef Harush                 |      Owner:  nobody
         Type:  Uncategorized                 |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.6
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 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)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22884>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/056.c9cf49f0576e9e706d82f6dc86d42f78%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to