From: Mike Bayer Date: Tue, 8 Aug 2017 17:30:49 +0000 (-0400) Subject: Add quoted_name to pg8000 py_types X-Git-Tag: origin~79 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=03560c4b83308719067ec635662c35f9a437fb7f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add quoted_name to pg8000 py_types Fixed bug where the pg8000 driver would fail if using :meth:`.MetaData.reflect` with a schema name, since the schema name would be sent as a "quoted_name" object that's a string subclass, which pg8000 doesn't recognize. The quoted_name type is added to pg8000's py_types collection on connect. Change-Id: Id0f838320cb66563685e094e4eae2d5116100d27 Fixes: #4041 --- diff --git a/doc/build/changelog/unreleased_12/4041.rst b/doc/build/changelog/unreleased_12/4041.rst new file mode 100644 index 0000000000..ebcb7236c9 --- /dev/null +++ b/doc/build/changelog/unreleased_12/4041.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, postgresql + :tickets: 4041 + + Fixed bug where the pg8000 driver would fail if using + :meth:`.MetaData.reflect` with a schema name, since the schema name would + be sent as a "quoted_name" object that's a string subclass, which pg8000 + doesn't recognize. The quoted_name type is added to pg8000's + py_types collection on connect. diff --git a/lib/sqlalchemy/dialects/postgresql/pg8000.py b/lib/sqlalchemy/dialects/postgresql/pg8000.py index 8c019a26da..5f6cd0a3f6 100644 --- a/lib/sqlalchemy/dialects/postgresql/pg8000.py +++ b/lib/sqlalchemy/dialects/postgresql/pg8000.py @@ -73,6 +73,7 @@ from .base import ( _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES) import re from sqlalchemy.dialects.postgresql.json import JSON +from ...sql.elements import quoted_name class _PGNumeric(sqltypes.Numeric): @@ -244,6 +245,11 @@ class PGDialect_pg8000(PGDialect): def on_connect(self): fns = [] + + def on_connect(conn): + conn.py_types[quoted_name] = conn.py_types[unicode] + fns.append(on_connect) + if self.client_encoding is not None: def on_connect(conn): self.set_client_encoding(conn, self.client_encoding) diff --git a/test/dialect/postgresql/test_dialect.py b/test/dialect/postgresql/test_dialect.py index 376ba081cf..27e0ac0659 100644 --- a/test/dialect/postgresql/test_dialect.py +++ b/test/dialect/postgresql/test_dialect.py @@ -8,7 +8,8 @@ from sqlalchemy import testing import datetime from sqlalchemy import ( Table, Column, select, MetaData, text, Integer, String, Sequence, Numeric, - DateTime, BigInteger, func, extract, SmallInteger, TypeDecorator) + DateTime, BigInteger, func, extract, SmallInteger, TypeDecorator, literal, + cast) from sqlalchemy import exc, schema from sqlalchemy.dialects.postgresql import base as postgresql import logging @@ -225,6 +226,20 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): finally: users.drop() + def test_quoted_name_bindparam_ok(self): + from sqlalchemy.sql.elements import quoted_name + + with testing.db.connect() as conn: + eq_( + conn.scalar( + select( + [cast( + literal(quoted_name("some_name", False)), String)] + ) + ), + "some_name" + ) + def test_preexecute_passivedefault(self): """test that when we get a primary key column back from reflecting a table which has a default value on it, we pre-