I ran into the problem of being unable to read models that had fields
declared as time type, the fields
showed a 'Invalid type NoneType' , when using the MySQL backend, I dug into
the issue and the
cause was that the the method 'database.dictfetchall' was returning a
'datetime.timedelta'
object instead of a 'datetime.time' time object. to fix this I added a
converter function in the
'backend.mysql.database.py' file and assigned it to the TIME type .
Here is the patch file for the fix.
Tryton Version (Server-Client): 2.4.0
MySQL Version: 14.14 Distrib 5.1.16 for debian-linux-gnu
--
[email protected] mailing list
diff --git a/trytond/backend/mysql/database.py b/trytond/backend/mysql/database.py
index ef03769..4d328fb 100644
--- a/trytond/backend/mysql/database.py
+++ b/trytond/backend/mysql/database.py
@@ -5,13 +5,16 @@ from trytond.config import CONFIG
import MySQLdb
import MySQLdb.cursors
import MySQLdb.converters
+
from MySQLdb import IntegrityError as DatabaseIntegrityError
from MySQLdb import OperationalError as DatabaseOperationalError
import logging
import os
import re
import time
+import datetime
import tempfile
+import logging
QUOTE_SEPARATION = re.compile(r"(.*?)('.*?')", re.DOTALL)
EXTRACT_EPOCH_PATTERN = re.compile(r'EXTRACT\s*\(\s*EPOCH\s+FROM',
@@ -30,6 +33,11 @@ def _replace_split_part_right(mobj):
return ', -1'
return ', 1'
+def time_converter(v):
+ """This function converts a string to a datetime.time object instance"""
+ h, m, s = tuple([int(t) for t in v.split(':')])
+ return datetime.time(h, m, s)
+
class Database(DatabaseInterface):
@@ -42,6 +50,8 @@ class Database(DatabaseInterface):
def cursor(self, autocommit=False, readonly=False):
conv = MySQLdb.converters.conversions.copy()
conv[float] = lambda value, _: repr(value)
+ conv[MySQLdb.constants.FIELD_TYPE.TIME] = time_converter
+
args = {
'db': self.database_name,
'sql_mode': 'traditional,postgresql',