]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed bug whereby a DBAPI that can return "0"
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 23 Mar 2013 23:00:11 +0000 (19:00 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 23 Mar 2013 23:00:11 +0000 (19:00 -0400)
for cursor.lastrowid would not function correctly
in conjunction with :attr:`.ResultProxy.inserted_primary_key`.

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/engine/default.py
test/sql/test_query.py

index 0f174ab51b805007121a18f18af6e564ed672596..b59ad07f7c8c00d33b3d39d413551ce28fb240c6 100644 (file)
@@ -6,6 +6,13 @@
 .. changelog::
     :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
index 1db0f2ce4a69ce881ee7506d6aaf639379296d97..4c49e58f6b6336fe94a28c62248ede844cd765dc 100644 (file)
@@ -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)
index b5f50aeea71590c30f1673691ae446ed9cab259d..956a1165c32a32dca90792ea632648661ea81ff6 100644 (file)
@@ -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,