Opened 11 years ago

Closed 11 years ago

#22884 closed Uncategorized (duplicate)

MySQL, unmanaged entity, does not get id after save()

Reported by: Jossef Harush Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)

Change History (2)

comment:1 by Tim Graham, 11 years ago

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.

comment:2 by Claude Paroz, 11 years ago

Resolution: duplicate
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top