我认为您显示的类型装饰器有几个问题。
impl
应该sqlalchemy.types.Integer
代替DateTime
。- 装饰器应允许可空列。
这就是我的想法:
import datetime, timefrom sqlalchemy.types import TypeDecorator, DateTime, Integerclass IntegerDateTime(TypeDecorator): """a type that decorates DateTime, converts to unix time on the way in and to datetime.datetime objects on the way out.""" impl = Integer # In schema, you want these datetimes to # be stored as integers. def process_bind_param(self, value, _): """Assumes a datetime.datetime""" if value is None: return None # support nullability elif isinstance(value, datetime.datetime): return int(time.mktime(value.timetuple())) raise ValueError("Can operate only on datetime values. " "Offending value type: {0}".format(type(value).__name__)) def process_result_value(self, value, _): if value is not None: # support nullability return datetime.datetime.fromtimestamp(float(value))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)