2

PostgreSQL 支持使用此处提到的 DateStyle 属性指定日期格式, http://www.postgresql.org/docs/current/interactive/runtime-config-client.html#GUC-DATESTYLE。(链接最初是 8.3 版本的文档)。

我找不到有关如何定义此属性的任何 SQLAlchemy ORM 文档参考。有可能做到吗?

4

1 回答 1

4

SQLAlchemy 使用 DBAPI(通常是 psycopg2)来编组与python 日期时间对象之间的日期值 - 然后您可以使用标准 python 技术以任何方式格式化/解析。因此不需要数据库端的日期格式化功能。

如果你想设置这个变量,你可以执行 PG 的 SET 语句:

conn = engine.connect()
conn.execute("SET DateStyle='somestring'")
# work with conn

使这个全局连接到所有连接:

from sqlalchemy import event
from sqlalchemy.engine import Engine

@event.listens_for(Engine, "connect")
def connect(dbapi_connection, connection_record):
    cursor = dbapi_connection.cursor()
    cursor.execute("SET DateStyle='somestring'")
    cursor.close()
于 2013-04-10T22:44:44.590 回答