From 74f9d5163f4857475236bebec9ef0d65ac224886 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 28 Jan 2021 14:53:02 -0500 Subject: Render NULL for bindparam w/ None value/literal_binds, warn Adjusted the "literal_binds" feature of :class:`_sql.Compiler` to render NULL for a bound parameter that has ``None`` as the value, either explicitly passed or omitted. The previous error message "bind parameter without a renderable value" is removed, and a missing or ``None`` value will now render NULL in all cases. Previously, rendering of NULL was starting to happen for DML statements due to internal refactorings, but was not explicitly part of test coverage, which it now is. While no error is raised, when the context is within that of a column comparison, and the operator is not "IS"/"IS NOT", a warning is emitted that this is not generally useful from a SQL perspective. Fixes: #5888 Change-Id: Id5939d8dbfb1156a9f8a7f7e76cf18327155331a --- lib/sqlalchemy/sql/compiler.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'lib/sqlalchemy/sql/compiler.py') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index aabc257eb..353de2c48 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2055,6 +2055,7 @@ class SQLCompiler(Compiled): _in_binary = kw.get("_in_binary", False) kw["_in_binary"] = True + kw["_binary_op"] = binary.operator text = ( binary.left._compiler_dispatch( self, eager_grouping=eager_grouping, **kw @@ -2306,10 +2307,15 @@ class SQLCompiler(Compiled): value = render_literal_value else: if bindparam.value is None and bindparam.callable is None: - raise exc.CompileError( - "Bind parameter '%s' without a " - "renderable value not allowed here." % bindparam.key - ) + op = kw.get("_binary_op", None) + if op and op not in (operators.is_, operators.is_not): + util.warn_limited( + "Bound parameter '%s' rendering literal NULL in a SQL " + "expression; comparisons to NULL should not use " + "operators outside of 'is' or 'is not'", + (bindparam.key,), + ) + return self.process(sqltypes.NULLTYPE, **kw) value = bindparam.effective_value if bindparam.expanding: -- cgit v1.2.1