[Django] #22884: MySQL, unmanaged entity, does not get id after save()

12 views
Skip to first unread message

Django

unread,
Jun 23, 2014, 4:33:52 AM6/23/14
to django-...@googlegroups.com
#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.

Django

unread,
Jun 23, 2014, 10:22:22 AM6/23/14
to django-...@googlegroups.com
#22884: MySQL, unmanaged entity, does not get id after save()
-------------------------------------+-------------------------------------

Reporter: Jossef Harush | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.6
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Can you write a test for Django's test suite that reproduces the error?

The RuntimeWarnings is an issue with your code, see
https://docs.djangoproject.com/en/1.6/topics/i18n/timezones/#code.

--
Ticket URL: <https://code.djangoproject.com/ticket/22884#comment:1>

Django

unread,
Jun 23, 2014, 11:18:28 AM6/23/14
to django-...@googlegroups.com
#22884: MySQL, unmanaged entity, does not get id after save()
-------------------------------------+-------------------------------------

Reporter: Jossef Harush | Owner: nobody
Type: Uncategorized | Status: closed

Component: Database layer | Version: 1.6
(models, ORM) | Resolution: duplicate

Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* status: new => closed
* resolution: => duplicate


Comment:

This is expected behavior, core backends introspection is currently based
on PEP 249's cursor.description, which doesn't allow us to introspect if
the field is autoincrement or not. After fixing #21907, the output will
now at least output a hint "# AutoField?" after the field to suggest the
field type change during review.

--
Ticket URL: <https://code.djangoproject.com/ticket/22884#comment:2>

Reply all
Reply to author
Forward
0 new messages