summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-03-23 19:00:11 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-03-23 19:00:11 -0400
commit35c5fd3fba57a04ebd9083207875692bb92ac6d4 (patch)
treea7a3b968866e8af717ee66894004c4600e5e3dd6
parente69b9542a81a50545e4d4147c2053d126f4fe250 (diff)
downloadsqlalchemy-35c5fd3fba57a04ebd9083207875692bb92ac6d4.tar.gz
Fixed bug whereby a DBAPI that can return "0"
for cursor.lastrowid would not function correctly in conjunction with :attr:`.ResultProxy.inserted_primary_key`.
-rw-r--r--doc/build/changelog/changelog_08.rst7
-rw-r--r--lib/sqlalchemy/engine/default.py2
-rw-r--r--test/sql/test_query.py19
3 files changed, 26 insertions, 2 deletions
diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst
index 0f174ab51..b59ad07f7 100644
--- a/doc/build/changelog/changelog_08.rst
+++ b/doc/build/changelog/changelog_08.rst
@@ -7,6 +7,13 @@
:version: 0.8.1
.. change::
+ :tags: bug, sql
+
+ Fixed bug whereby a DBAPI that can return "0"
+ for cursor.lastrowid would not function correctly
+ in conjunction with :attr:`.ResultProxy.inserted_primary_key`.
+
+ .. change::
:tags: bug, mssql
:tickets: 2683
:pullreq: 46
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py
index 1db0f2ce4..4c49e58f6 100644
--- a/lib/sqlalchemy/engine/default.py
+++ b/lib/sqlalchemy/engine/default.py
@@ -679,7 +679,7 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
lastrowid = proc(lastrowid)
self.inserted_primary_key = [
- c is autoinc_col and lastrowid or v
+ lastrowid if c is autoinc_col else v
for c, v in zip(
table.primary_key,
self.inserted_primary_key)
diff --git a/test/sql/test_query.py b/test/sql/test_query.py
index b5f50aeea..956a1165c 100644
--- a/test/sql/test_query.py
+++ b/test/sql/test_query.py
@@ -190,10 +190,27 @@ class QueryTest(fixtures.TestBase):
try:
table.create(bind=engine, checkfirst=True)
i = insert_values(engine, table, values)
- assert i == assertvalues, "tablename: %s %r %r" % (table.name, repr(i), repr(assertvalues))
+ assert i == assertvalues, "tablename: %s %r %r" %
+ (table.name, repr(i), repr(assertvalues))
finally:
table.drop(bind=engine)
+ @testing.only_on('sqlite+pysqlite')
+ @testing.provide_metadata
+ def test_lastrowid_zero(self):
+ from sqlalchemy.dialects import sqlite
+ eng = engines.testing_engine()
+ class ExcCtx(sqlite.base.SQLiteExecutionContext):
+ def get_lastrowid(self):
+ return 0
+ eng.dialect.execution_ctx_cls = ExcCtx
+ t = Table('t', MetaData(), Column('x', Integer, primary_key=True),
+ Column('y', Integer))
+ t.create(eng)
+ r = eng.execute(t.insert().values(y=5))
+ eq_(r.inserted_primary_key, [0])
+
+
@testing.fails_on('sqlite', "sqlite autoincremnt doesn't work with composite pks")
def test_misordered_lastrow(self):
related = Table('related', metadata,