查询失败-传递日期时间时[接收到不兼容的日期时间]?

时间:2018-09-10 11:10:53

标签: python mysql pymysql

代码

    variables = {
        "code" : code,
        "distributor" : distributor,
        "license_type" : license_type,
        "one_time_usage" : one_time_usage,
        "valid_from": valid_from, 
        "valid_to": valid_to, 
        "user_id": user_id,
    }
    log.debug(variables)

    sql_statement = ('INSERT INTO {}'
                    '(code, license_type, distributor, one_time_usage, created, valid_from, valid_to, user_id) '
                    'values '
                    '(%(code)s, %(license_type)s, %(distributor)s, %(one_time_usage)s, NOW(), %(valid_from)s, %(valid_to)s, %(user_id)s)'
                    ';'.format(LICENSE_CODES_TABLE))

    log.debug(sql_statement)
    log.debug(variables)

    cur = db.cursor()
    success = cur.execute(sql_statement, variables)

调试消息

[DEBUG] INSERT INTO license_codes(code, license_type, distributor, one_time_usage, created, valid_from, valid_to, user_id) values (%(code)s, %(license_type)s, %(distributor)s, %(one_time_usage)s, NOW(), %(valid_from)s, %(valid_to)s, %(user_id)s);
[DEBUG] {'code': 'JWFF9-MJK2H-VQK2B-72CYM-M2XDM', 'user_id': u'jake', 'valid_to': datetime.datetime(2020, 10, 10, 0, 0), 'license_type': u'Premium', 'valid_from': datetime.datetime(2010, 1, 1, 0, 0), 'one_time_usage': u'N', 'distributor': u'test'}

错误

errors: [AssertionError('Received not compatible datetime "u\'20100101\'"',), AssertionError('Received not compatible datetime "u\'20201010\'"',)]


[ERROR] 2018-09-10T11:01:57.384Z    Traceback (most recent call last):
File "/var/task/graphql/execution/executor.py", line 476, in complete_value_catching_error
exe_context, return_type, field_asts, info, path, result
File "/var/task/graphql/execution/executor.py", line 556, in complete_value
return complete_leaf_value(return_type, path, result)
File "/var/task/graphql/execution/executor.py", line 615, in complete_leaf_value
serialized_result = return_type.serialize(result)
File "/var/task/graphene/types/datetime.py", line 28, in serialize
'Received not compatible datetime "
{}
"'.format(repr(dt))
AssertionError: Received not compatible datetime "u'20100101'"

我也尝试过传递"valid_from": valid_from.isoformat()"valid_from": valid_from.datetime,但没有成功。

1 个答案:

答案 0 :(得分:0)

我在使用 graphene 时遇到了 Received not compatible datetime 错误(我相信是日期 serialization 错误)。

我使用 dateutil 的 parser.parse 解决了这个问题,将我用于日期的 string 解析为 datetime