From fa0666cb04174cdd2592ab1327d48e431fe86ffa Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 19 Mar 2023 10:37:15 -0400 Subject: dont render VARCHAR length for PG casts Fixed critical regression in PostgreSQL dialects such as asyncpg which rely upon explicit casts in SQL in order for datatypes to be passed to the driver correctly, where a :class:`.String` datatype would be cast along with the exact column length being compared, leading to implicit truncation when comparing a ``VARCHAR`` of a smaller length to a string of greater length regardless of operator in use (e.g. LIKE, MATCH, etc.). The PostgreSQL dialect now omits the length from ``VARCHAR`` when rendering these casts. Fixes: #9511 Change-Id: If094146d8cfd989a0b780872f38e86fd41ebfec2 --- lib/sqlalchemy/dialects/postgresql/base.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/sqlalchemy/dialects/postgresql/base.py') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 47a516a5a..18f31ce47 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1717,6 +1717,10 @@ class PGCompiler(compiler.SQLCompiler): return f"{element.name}{self.function_argspec(element, **kw)}" def render_bind_cast(self, type_, dbapi_type, sqltext): + if dbapi_type._type_affinity is sqltypes.String: + # use VARCHAR with no length for VARCHAR cast. + # see #9511 + dbapi_type = sqltypes.STRINGTYPE return f"""{sqltext}::{ self.dialect.type_compiler_instance.process( dbapi_type, identifier_preparer=self.preparer -- cgit v1.2.1