Hello,
If I read Cristiano correctly, he does this:
INSERT INTO my_table (foo_date) VALUES ('2015-12-24 12:34:56.123456');
SELECT foo_date FROM my_table WHERE foo_date = '2015-12-24 12:34:56.123456’;
and he expects it to return the row he just inserted. That wouldn’t be astonishing. In my opinions, either the insert should fail on the select should return the inserted row. (Obviously this isn’t what happens. I kno\ we’re talking about MySQL here.)
The relevant commits landed in 1.8 which was released in April:
https://github.com/django/django/commit/9e746c13e81241fbf1ae64ec118edaa491790046
https://github.com/django/django/commit/22da5f8817ffff3917bcf8a652dce84f382c9202
The release notes say that “new datetime database columns created with Django 1.8 and MySQL 5.6.4 and up will support microseconds”. This means that users end up with “mixed” databases where some date time columns have fractional microseconds and others have not. I don’t think that’s a very good outcome.
I had brought up that question on the ticket,
https://code.djangoproject.com/ticket/19716#comment:3 : “what's going to happen for developers upgrading from an older version of Django?” but I didn’t follow up, unfortunately.
Depending on how MySQL handles conversions, this can easily result in bad behavior. For example, let’s assume the following model:
class Foo(models.Model):
updated_at = models.DateTimeField() # field created on old Django or MySQL
admin_updated_at = models.DateTimeField(blank=True, null=True) # field created on newer Django or MySQL
@property
def last_updated_by_admin(self):
return updated_at == admin_updated_at
foo = Foo.objects.get(…)
foo.updated_at = foo.admin_updated_at = timezone.now()
foo.save()
foo.last_updated_by_admin # True
foo = Foo.objects.get(…)
foo.last_updated_by_admin # False
To be fair, this has a lot to do with MySQL’s lax approach to storing data. There’s so many situations where it just throws data away happily that one can’t really expect to read back data written to MySQL.
That said, this change in Django sets up this trap for unsuspecting users. Providing a way for users to declare which fields use the old format won't work because of pluggable apps: they cannot know what version of MySQL their users were running when they first created a given datetime column. The best solution may be to provide a conversion script to upgrade all datetime columns from the old to the new format.
Best regards,
--
Aymeric.