Hi,
I'm trying to have a deleted_at column on my records. I use MySQL and
latest sqlalchemy as of writing. For historical reasons, I want to keep
using the MySQL's TIMESTAMP columns.
I would like to use timezone-aware datetime throughout my codebase.
Everything in my DB is stored as UTC, so I don't really need to store the
timezone. I just want to make sure the datetime is returned as a
UTC-datetime.
import pytz
from sqlalchemy import Column, text, types
from sqlalchemy.dialects.mysql import TIMESTAMP as M_TIMESTAMP
# Fractional second precision
FSP = 6
TIMESTAMP = M_TIMESTAMP(fsp=FSP)
CURRENT_TIMESTAMP = text("CURRENT_TIMESTAMP(%d)" % FSP)
class TimezoneAwareTimestamp(types.TypeDecorator):
"""Ensure tz-aware timestamp are returned."""
impl = TIMESTAMP
def process_result_value(self, value, dialect):
if not value:
return None
return value.replace(tzinfo=pytz.UTC)
def DeletedAt():
return Column("deleted_at", TimezoneAwareTimestamp, server_onupdate=text
("0"),
nullable=True)
This is a pretty natural solution I came up with. Problem:
TimezoneAwareTimestamp does not respect the `server_onupdate` attribute on
creation. A table created with this DeletedAt column will show up as:
deleted_at` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE
CURRENT_TIMESTAMP(6)
instead of (replacing TimezoneAwareTimestamp with TIMESTAMP):
deleted_at` timestamp(6) NULL DEFAULT NULL
How can I have the custom type respect server_onupdate and nullable?
Thanks,
Charles
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/983a49db-ae3f-422a-b996-b176f76c5aa7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.