0

Code

    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 messages

[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'}

Error

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'"

I have also tried passing "valid_from": valid_from.isoformat() and "valid_from": valid_from.datetime with no success.

user1283776
  • 19,640
  • 49
  • 136
  • 276
  • can't you use `0000-00-00 00:00:00` instead of `None`? – Abhishek Keshri Sep 10 '18 at 11:54
  • Have you had a look at this question? https://stackoverflow.com/questions/14291636/what-is-the-proper-way-to-convert-between-mysql-datetime-and-python-timestamp#14291826 – MTTI Sep 10 '18 at 11:56

1 Answers1

0

I got Received not compatible datetime error using graphene (I believe, as a date serialization error).

I solved that with dateutil's parser.parse to parse the string I was using for date, into a datetime.

cellepo
  • 4,001
  • 2
  • 38
  • 57