This is really very interesting question. I looked through the source code and here is the reason for setting the datetime
with fractional seconds. The following snippet is from the file django/db/backends/mysql/base.py
:
class DatabaseWrapper(BaseDatabaseWrapper):
vendor = 'mysql'
# This dictionary maps Field objects to their associated MySQL column
# types, as strings. Column-type strings can contain format strings; they'll
# be interpolated against the values of Field.__dict__ before being output.
# If a column type is set to None, it won't be included in the output.
_data_types = {
'AutoField': 'integer AUTO_INCREMENT',
'BinaryField': 'longblob',
'BooleanField': 'bool',
'CharField': 'varchar(%(max_length)s)',
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
'DateField': 'date',
'DateTimeField': 'datetime',
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
'DurationField': 'bigint',
'FileField': 'varchar(%(max_length)s)',
'FilePathField': 'varchar(%(max_length)s)',
'FloatField': 'double precision',
'IntegerField': 'integer',
'BigIntegerField': 'bigint',
'IPAddressField': 'char(15)',
'GenericIPAddressField': 'char(39)',
'NullBooleanField': 'bool',
'OneToOneField': 'integer',
'PositiveIntegerField': 'integer UNSIGNED',
'PositiveSmallIntegerField': 'smallint UNSIGNED',
'SlugField': 'varchar(%(max_length)s)',
'SmallIntegerField': 'smallint',
'TextField': 'longtext',
'TimeField': 'time',
'UUIDField': 'char(32)',
}
@cached_property
def data_types(self):
if self.features.supports_microsecond_precision:
return dict(self._data_types, DateTimeField='datetime(6)', TimeField='time(6)')
else:
return self._data_types
# ... further class methods
In the method data_types
the if condition checks the MySQL version. The method supports_microsecond_precision
comes from the file django/db/backends/mysql/features.py
:
class DatabaseFeatures(BaseDatabaseFeatures):
# ... properties and methods
def supports_microsecond_precision(self):
# See https://github.com/farcepest/MySQLdb1/issues/24 for the reason
# about requiring MySQLdb 1.2.5
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
So when you use MySQL 5.6.4 or higher the field DateTimeField
is mapped to datetime(6)
.
I couldn't find any possibility given by Django to adjust this, so ended up with monkey patching:
from django.db.backends.mysql.base import DatabaseWrapper
DatabaseWrapper.data_types = DatabaseWrapper._data_types
Put the above code where it suits best your needs, be it models.py
or __init__.py
, or maybe some other file.
When running migrations Django will create column datetime
and not datetime(6)
for DateTimeField
, even if you're using MySQL 5.7.
DATETIME
format in MySQL? Judging by the name of the attributecreate_time
, you'd probably needTIMESTAMP
in MySQL. If you want to save automatically the creation time you can change your statement to:models.DateTimeField(auto_now_add=True)
. – cezar Oct 3 '17 at 8:24auto_now_add
store data with milliseconds. He doesn't want to store this time in his database. – Essex Oct 3 '17 at 8:38datetime
out ofDateTimeField
(which sounds logically). I have checked my code with MySQL 5.5 and there it is justdatetime
. Probably it is since MySQL 5.7 that this field is saved asdatetime(6)
. – cezar Oct 3 '17 at 8:53