【问题标题】:Broken datetime on CloudSQL + Google App EngineCloudSQL + Google App Engine 上的日期时间损坏
【发布时间】:2016-05-19 09:53:29
【问题描述】:

所以我在 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)

不知道我还能做些什么,这个例子已经尽可能地简化了。有谁知道发生了什么?太糟糕了,谷歌通常无法拖延。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-29
        • 2013-07-15
        • 1970-01-01
        • 2021-07-12
        • 2013-10-14
        • 2021-11-18
        相关资源
        最近更新 更多