From cb19f227f31701322f398b5bdd77427dfc81da0d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 2 Feb 2013 16:17:58 -0500 Subject: [PATCH] The :meth:`.ColumnOperators.in_` operator will now coerce values of ``None`` to :func:`.null`. [ticket:2496] --- doc/build/changelog/changelog_08.rst | 7 +++++++ lib/sqlalchemy/sql/expression.py | 2 ++ test/dialect/test_mssql.py | 4 ++++ test/sql/test_operators.py | 6 ++++++ 4 files changed, 19 insertions(+) diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index e6cc169b1d..259c59243e 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -6,6 +6,13 @@ .. changelog:: :version: 0.8.0 + .. change:: + :tags: bug, sql + :tickets: 2496 + + The :meth:`.ColumnOperators.in_` operator will now coerce + values of ``None`` to :func:`.null`. + .. change:: :tags: feature, sql :tickets: 2657 diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index de728f77b7..90837f4ab3 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -2084,6 +2084,8 @@ class _DefaultColumnComparator(operators.ColumnOperators): raise exc.InvalidRequestError('in() function accept' 's either a list of non-selectable values, ' 'or a selectable: %r' % o) + elif o is None: + o = null() else: o = expr._bind_param(op, o) args.append(o) diff --git a/test/dialect/test_mssql.py b/test/dialect/test_mssql.py index 210f8c7489..52ba77310b 100644 --- a/test/dialect/test_mssql.py +++ b/test/dialect/test_mssql.py @@ -171,6 +171,10 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): select([t]).where(t.c.foo.in_(['x', 'y', 'z'])), "SELECT sometable.foo FROM sometable WHERE sometable.foo " "IN ('x', 'y', 'z')", + ), + ( + t.c.foo.in_([None]), + "sometable.foo IN (NULL)" ) ]: self.assert_compile(expr, compile, dialect=mxodbc_dialect) diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index 45f4978ed1..7215ae5652 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -736,6 +736,12 @@ class InTest(fixtures.TestBase, testing.AssertsCompiledSQL): {'param_1': 10} ) + def test_in_28(self): + self.assert_compile( + self.table1.c.myid.in_([None]), + "mytable.myid IN (NULL)" + ) + class MathOperatorTest(fixtures.TestBase, testing.AssertsCompiledSQL): __dialect__ = 'default' -- 2.47.2