From: Mike Bayer Date: Sat, 23 Mar 2013 23:00:11 +0000 (-0400) Subject: Fixed bug whereby a DBAPI that can return "0" X-Git-Tag: rel_0_8_1~26^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35c5fd3fba57a04ebd9083207875692bb92ac6d4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fixed bug whereby a DBAPI that can return "0" for cursor.lastrowid would not function correctly in conjunction with :attr:`.ResultProxy.inserted_primary_key`. --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 0f174ab51b..b59ad07f7c 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -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 diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 1db0f2ce4a..4c49e58f6b 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 b5f50aeea7..956a1165c3 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,