sqlite 不支持毫秒怎么办,可以用sqlalchemy自定义类型

from sqlalchemy import DECIMAL, Index, String, Date, Integer, Text, CHAR, SmallInteger, Float, Time, case, and_, extract, Boolean, Enum, TypeDecorator

# 自定义类型
class DateTimeString(TypeDecorator):
    impl = String

    def process_bind_param(self, value, dialect):
        if value is not None:
            return value.strftime('%Y-%m-%d %H:%M:%S.%f')
        return value

    def process_result_value(self, value, dialect):
        if value is not None:
            return datetime.strptime(value, '%Y-%m-%d %H:%M:%S.%f')
        return value

后续实现orm的时候字段为
localtime: Mapped[datetime] = mapped_column(DateTimeString(), nullable=True)

posted @ 2024-05-25 23:09  meizhengchao  阅读(17)  评论(0编辑  收藏  举报