mysql 指针不合法_Django | Mysql 返回不合法的日期时间对象

1 错误描述

在查询数据集中的日期时间对象时

In [38]: Device.objects.datetimes('latest_alarm_time', 'month')

Out[38]: SELECT DISTINCT

CAST(DATE_FORMAT(CONVERT_TZ(`device_device`.`latest_alarm_time`, 'UTC', 'Asia/Shanghai'), '%Y-%m-01 00:00:00') AS DATETIME) AS `datetimefield` FROM `device_device` WHERE `device_device`.`latest_alarm_time` IS NOT NULL ORDER BY `datetimefield` ASC LIMIT 21

然后报错

ValueError: Database returned an invalid datetime value. Are time zone definitions for your database installed?

2 解决问题

实际情况,数据库中是有数据,目测月份提取失败;到 mysql 执行了下

mysql root@localhost:py365> select convert_tz('2018-05-10 12:30:00', 'UTC', 'Asia/Shanghai');

+-------------------------------------------------------------+

| convert_tz('2018-05-10 12:30:00', 'UTC', 'Asia/Shanghai') |

|-------------------------------------------------------------|

| NULL |

+-------------------------------------------------------------+

果然,结果返回令人诧异的 NULL

看了下 Django orm 的 datetimes 官方文档

Note

This function performs time zone conversions directly in the database. As a consequence, your database must be able to interpret the value of tzinfo.tzname(None). This translates into the following requirements:

SQLite: no requirements. Conversions are performed in Python with pytz (installed when you install Django).

PostgreSQL: no requirements (see Time Zones).

Oracle: no requirements (see Choosing a Time Zone File).

MySQL: load the time zone tables with mysql_tzinfo_to_sql.

即 mysql 需要使用 mysql_tzinfo_to_sql 载入时区表,接着跳到 https://dev.mysql.com/doc/ref...

按照 mysql 官方的文档

For the first invocation syntax, pass the zoneinfo directory path name to mysql_tzinfo_to_sql and send the output into the mysql program. For example:

我需要按照以下命令执行

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

然后再次执行上面执行过的转换语句

mysql root@localhost:py365> select convert_tz('2018-05-10 12:30:00', 'UTC', 'Asia/Shanghai');

+-------------------------------------------------------------+

| convert_tz('2018-05-10 12:30:00', 'UTC', 'Asia/Shanghai') |

|-------------------------------------------------------------|

| 2018-05-10 20:30:00 |

+-------------------------------------------------------------+

yes,返回了正确的结果;

在 shell 中 执行数据库查询语句

In [45]: Device.objects.datetimes('latest_alarm_time', 'month')

Out[45]: SELECT DISTINCT CAST(DATE_FORMAT(CONVERT_TZ(`device_device`.`latest_alarm_time`, 'UTC', 'Asia/Shanghai'), '%Y-%m-01 00:00:00') AS DATETIME) AS `datetimefield` FROM `device_device` WHERE `device_device`.`latest_alarm_time` IS NOT NULL ORDER BY `datetimefield` ASC LIMIT 21

Execution time: 0.000591s [Database: default]

)]>

正常,so 问题解决,看来还得认真看文档呀

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值