rgz*_*rgz 3 python mysql sqlalchemy
背景,有几种方法可以在MySQ中存储日期.
作为MySQL Date字段,另一种mysql特定的数据类型.
不要将案例2与案例3(或案例4)混淆是非常重要的.我有一个带有整数日期字段的现有表(案例2)如何在sqlalchemy中以一种我不必访问mysql的"FROM_UNIXTIME"函数的方式定义它?
对于记录,只使用sqlalchemy.types.DateTime并希望它在检测到整数列时做正确的事情不起作用,它适用于时间戳字段和日期字段.
我认为您展示的类型装饰器存在一些问题.
impl应该sqlalchemy.types.Integer而不是DateTime.这就是我的想法:
import datetime, time
from sqlalchemy.types import TypeDecorator, DateTime, Integer
class 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))
Run Code Online (Sandbox Code Playgroud)