From 9609f5ffb52ce8a4969059e299773ac7176dbb0d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 3 Apr 2017 17:25:26 -0400 Subject: ResultProxy won't autoclose connection until state flag is set Changed the mechanics of :class:`.ResultProxy` to unconditionally delay the "autoclose" step until the :class:`.Connection` is done with the object; in the case where Postgresql ON CONFLICT with RETURNING returns no rows, autoclose was occurring in this previously non-existent use case, causing the usual autocommit behavior that occurs unconditionally upon INSERT/UPDATE/DELETE to fail. Change-Id: I235a25daf4381b31f523331f810ea04450349722 Fixes: #3955 (cherry picked from commit 8ee363e4917b0dcd64a83b6d26e465c9e61e0ea5) (cherry picked from commit f52fb5282a046d26b6ee2778e03b995eb117c2ee) --- test/dialect/postgresql/test_on_conflict.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test/dialect') diff --git a/test/dialect/postgresql/test_on_conflict.py b/test/dialect/postgresql/test_on_conflict.py index 7c83f2826..c3e1b9158 100644 --- a/test/dialect/postgresql/test_on_conflict.py +++ b/test/dialect/postgresql/test_on_conflict.py @@ -12,6 +12,7 @@ class OnConflictTest(fixtures.TablesTest): __only_on__ = 'postgresql >= 9.5', __backend__ = True + run_define_tables = 'each' @classmethod def define_tables(cls, metadata): @@ -79,6 +80,7 @@ class OnConflictTest(fixtures.TablesTest): with testing.db.connect() as conn: result = conn.execute( insert(users).on_conflict_do_nothing(), + dict(id=1, name='name1') ) eq_(result.inserted_primary_key, [1]) @@ -96,6 +98,33 @@ class OnConflictTest(fixtures.TablesTest): [(1, 'name1')] ) + def test_on_conflict_do_nothing_connectionless(self): + users = self.tables.users_xtra + + with testing.db.connect() as conn: + result = conn.execute( + insert(users).on_conflict_do_nothing( + constraint='uq_login_email'), + + dict(name='name1', login_email='email1') + ) + eq_(result.inserted_primary_key, [1]) + eq_(result.returned_defaults, (1,)) + + result = testing.db.execute( + insert(users).on_conflict_do_nothing( + constraint='uq_login_email' + ), + dict(name='name2', login_email='email1') + ) + eq_(result.inserted_primary_key, None) + eq_(result.returned_defaults, None) + + eq_( + testing.db.execute(users.select().where(users.c.id == 1)).fetchall(), + [(1, 'name1', 'email1', None)] + ) + @testing.provide_metadata def test_on_conflict_do_nothing_target(self): users = self.tables.users -- cgit v1.2.1