CloudSQL + Google App Engine上的日期时间损坏

时间:2016-02-09 12:03:30

标签: google-app-engine mysql-python google-cloud-sql

所以我在App Engine上的Django中遇到了很多日期时间的问题,但后来我开始追踪错误,似乎有更严重的问题。这就是我所看到的。

这是我的数据库:

mysql> select * from polls_question;
+----+---------------+----------------------------+
| id | question_text | pub_date                   |
+----+---------------+----------------------------+
|  1 | test          | 2016-02-08 15:24:44.000000 |
+----+---------------+----------------------------+
1 row in set (0.16 sec)

以下是试图读取此数据的代码:

import MySQLdb
import os
import webapp2

class IndexPage(webapp2.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    env = os.getenv('SERVER_SOFTWARE')
    if (env and env.startswith('Google App Engine/')):
    # Connecting from App Engine
      db = MySQLdb.connect(
      unix_socket='/cloudsql/<removed>:<removed>',
      user='root',db='gaetest')
    else:
      # Connecting from an external network.
      # Make sure your network is whitelisted
      db = MySQLdb.connect(
      host='<removed>',
      port=3306,
      user='root', passwd='<removed>',db='<removed>')

    cursor = db.cursor()
    cursor.execute('SELECT * FROM polls_question')
    for r in cursor.fetchall():
      self.response.write('%s\n' % str(r))

    db.close()

app = webapp2.WSGIApplication([
    ('/', IndexPage),
    ])

在我的电脑上它给了我:

(1L, 'test', datetime.datetime(2016, 2, 8, 15, 24, 44))

当我访问远程网址时,我得到:

(1L, 'test', None)

不确定我还能做什么,这个例子就像它可以得到的那样简单。有没有人知道发生了什么?太糟糕了谷歌通常无法阻止。

2 个答案:

答案 0 :(得分:1)

所以在花了太多时间在此之后我发现了问题,数据库是使用django的migrate函数创建的,这导致它创建了一个datetime(6)(高精度),这些在访问时不起作用站点远程。正常的日期时间工作。所以改变它们会使它发挥作用。

问题的原因是我在本地使用了一个支持datetime(6)的更新的mysql-python lib。

答案 1 :(得分:1)

这是App Engine库MySQLdb 1.2.4b4的一个错误,在撰写本文时也是“最新的”。将MySQLdb的app.yaml依赖项从“最新”或“1.2.4b4”切换为“1.2.5”或“1.2.4”应该可以解决问题。