diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-03-15 11:16:21 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-03-15 11:16:48 -0400 |
commit | e81857d8dd1e253fa2a9d71e8867a0d5989ceda8 (patch) | |
tree | bb8f9250264503be6e80963cf2694f54cdfbf8db | |
parent | ccd546aac69015c6f27056d33bb235206f6e90f1 (diff) | |
download | sqlalchemy-e81857d8dd1e253fa2a9d71e8867a0d5989ceda8.tar.gz |
- add notes for pyodbc limitations with rowcount fixes #3675
(cherry picked from commit 8bc370ed382a45654101fa34bac4a2886ce089c3)
-rw-r--r-- | lib/sqlalchemy/dialects/mssql/base.py | 34 | ||||
-rw-r--r-- | lib/sqlalchemy/dialects/mssql/pyodbc.py | 7 |
2 files changed, 41 insertions, 0 deletions
diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 40bca670e..544890ead 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -337,6 +337,40 @@ Declarative form:: This option can also be specified engine-wide using the ``implicit_returning=False`` argument on :func:`.create_engine`. +.. _mssql_rowcount_versioning: + +Rowcount Support / ORM Versioning +--------------------------------- + +The SQL Server drivers have very limited ability to return the number +of rows updated from an UPDATE or DELETE statement. In particular, the +pymssql driver has no support, whereas the pyodbc driver can only return +this value under certain conditions. + +In particular, updated rowcount is not available when OUTPUT INSERTED +is used. This impacts the SQLAlchemy ORM's versioning feature when +server-side versioning schemes are used. When +using pyodbc, the "implicit_returning" flag needs to be set to false +for any ORM mapped class that uses a version_id column in conjunction with +a server-side version generator:: + + class MyTable(Base): + __tablename__ = 'mytable' + id = Column(Integer, primary_key=True) + stuff = Column(String(10)) + timestamp = Column(TIMESTAMP(), default=text('DEFAULT')) + __mapper_args__ = { + 'version_id_col': timestamp, + 'version_id_generator': False, + } + __table_args__ = { + 'implicit_returning': False + } + +Without the implicit_returning flag above, the UPDATE statement will +use ``OUTPUT inserted.timestamp`` and the rowcount will be returned as +-1, causing the versioning logic to fail. + Enabling Snapshot Isolation --------------------------- diff --git a/lib/sqlalchemy/dialects/mssql/pyodbc.py b/lib/sqlalchemy/dialects/mssql/pyodbc.py index d901e0ee1..36905004c 100644 --- a/lib/sqlalchemy/dialects/mssql/pyodbc.py +++ b/lib/sqlalchemy/dialects/mssql/pyodbc.py @@ -110,6 +110,13 @@ for unix + PyODBC. .. versionadded:: 0.7.7 ``supports_unicode_binds`` parameter to ``create_engine()``\ . +Rowcount Support +---------------- + +Pyodbc only has partial support for rowcount. See the notes at +:ref:`mssql_rowcount_versioning` for important notes when using ORM +versioning. + """ from .base import MSExecutionContext, MSDialect |