diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-04-26 19:17:06 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-04-26 20:49:38 -0400 |
commit | 802923062720589c0aea0d56a8672407be2cb79c (patch) | |
tree | b69913d5e6bd93f2f89c5ac8372f34dbeb265305 /lib/sqlalchemy/dialects/mssql | |
parent | cf329ec33153307968828b46a4466850ebcf374e (diff) | |
download | sqlalchemy-802923062720589c0aea0d56a8672407be2cb79c.tar.gz |
Pass all datetime values to pyodbc for timezone-naive column
Fixed regression caused by :ticket:`6306` which added support for
``DateTime(timezone=True)``, where the previous behavior of the pyodbc
driver of implicitly dropping the tzinfo from a timezone-aware date when
INSERTing into a timezone-naive DATETIME column were lost, leading to a SQL
Server error when inserting timezone-aware datetime objects into
timezone-native database columns.
Fixes: #6366
Change-Id: Id7821de13d75ede27f2165b37277a7223468dfa4
Diffstat (limited to 'lib/sqlalchemy/dialects/mssql')
-rw-r--r-- | lib/sqlalchemy/dialects/mssql/pyodbc.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/sqlalchemy/dialects/mssql/pyodbc.py b/lib/sqlalchemy/dialects/mssql/pyodbc.py index 424adadfc..95b2bb48e 100644 --- a/lib/sqlalchemy/dialects/mssql/pyodbc.py +++ b/lib/sqlalchemy/dialects/mssql/pyodbc.py @@ -353,6 +353,8 @@ class _ms_binary_pyodbc(object): class _ODBCDateTime(sqltypes.DateTime): """Add bind processors to handle datetimeoffset behaviors""" + has_tz = False + def bind_processor(self, dialect): def process(value): if value is None: @@ -360,7 +362,7 @@ class _ODBCDateTime(sqltypes.DateTime): elif isinstance(value, util.string_types): # if a string was passed directly, allow it through return value - elif isinstance(value, datetime.datetime) and value.tzinfo is None: + elif not value.tzinfo or (not self.timezone and not self.has_tz): # for DateTime(timezone=False) return value else: @@ -379,6 +381,10 @@ class _ODBCDateTime(sqltypes.DateTime): return process +class _ODBCDATETIMEOFFSET(_ODBCDateTime): + has_tz = True + + class _VARBINARY_pyodbc(_ms_binary_pyodbc, VARBINARY): pass @@ -453,7 +459,7 @@ class MSDialect_pyodbc(PyODBCConnector, MSDialect): BINARY: _BINARY_pyodbc, # support DateTime(timezone=True) sqltypes.DateTime: _ODBCDateTime, - DATETIMEOFFSET: _ODBCDateTime, + DATETIMEOFFSET: _ODBCDATETIMEOFFSET, # SQL Server dialect has a VARBINARY that is just to support # "deprecate_large_types" w/ VARBINARY(max), but also we must # handle the usual SQL standard VARBINARY |