]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add .as_generic() support to mssql BIT columns
authorGord Thompson <gord@gordthompson.com>
Thu, 22 Apr 2021 14:52:15 +0000 (08:52 -0600)
committerGord Thompson <gord@gordthompson.com>
Tue, 27 Apr 2021 19:12:53 +0000 (19:12 +0000)
Fixes: #6345
Change-Id: I2bdccc88e85c94d87519f58e474689ca7896f063

doc/build/changelog/unreleased_14/6345.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mssql/base.py
test/dialect/mssql/test_types.py

diff --git a/doc/build/changelog/unreleased_14/6345.rst b/doc/build/changelog/unreleased_14/6345.rst
new file mode 100644 (file)
index 0000000..152e38c
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, mssql, schema
+    :tickets: 6345
+
+    Add :meth:`_types.TypeEngine.as_generic` support for
+    :class:`sqlalchemy.dialects.mysql.BIT` columns, mapping
+    them to :class:`_sql.sqltypes.Boolean`.
index 8c2abc8e7b1c11e11a51841eecf466e5a1d370cd..e59221f8344e89b2091433f7360d603ce22d7be5 100644 (file)
@@ -1255,7 +1255,14 @@ class XML(sqltypes.Text):
     __visit_name__ = "XML"
 
 
-class BIT(sqltypes.TypeEngine):
+class BIT(sqltypes.Boolean):
+    """MSSQL BIT type.
+
+    Both pyodbc and pymssql return values from BIT columns as
+    Python <class 'bool'> so just subclass Boolean.
+
+    """
+
     __visit_name__ = "BIT"
 
 
index 4e78ec1220108a950116c4a9179cace9bdbe0d8a..798dbfc1fdc8318a370a436868251e7802a23c33 100644 (file)
@@ -36,6 +36,7 @@ from sqlalchemy.dialects.mssql import base as mssql
 from sqlalchemy.dialects.mssql import ROWVERSION
 from sqlalchemy.dialects.mssql import TIMESTAMP
 from sqlalchemy.dialects.mssql.base import _MSDate
+from sqlalchemy.dialects.mssql.base import BIT
 from sqlalchemy.dialects.mssql.base import DATETIMEOFFSET
 from sqlalchemy.dialects.mssql.base import MS_2005_VERSION
 from sqlalchemy.dialects.mssql.base import MS_2008_VERSION
@@ -1342,3 +1343,31 @@ class BinaryTest(fixtures.TestBase):
         stream = fp.read(len_)
         fp.close()
         return stream
+
+
+class BooleanTest(fixtures.TestBase, AssertsCompiledSQL):
+    __only_on__ = "mssql"
+
+    @testing.provide_metadata
+    @testing.combinations(
+        ("as_boolean_null", Boolean, True, "CREATE TABLE tbl (boo BIT NULL)"),
+        ("as_bit_null", BIT, True, "CREATE TABLE tbl (boo BIT NULL)"),
+        (
+            "as_boolean_not_null",
+            Boolean,
+            False,
+            "CREATE TABLE tbl (boo BIT NOT NULL)",
+        ),
+        ("as_bit_not_null", BIT, False, "CREATE TABLE tbl (boo BIT NOT NULL)"),
+        id_="iaaa",
+        argnames="col_type, is_nullable, ddl",
+    )
+    def test_boolean_as_bit(self, col_type, is_nullable, ddl):
+        tbl = Table(
+            "tbl", self.metadata, Column("boo", col_type, nullable=is_nullable)
+        )
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            ddl,
+        )
+        assert isinstance(tbl.c.boo.type.as_generic(), Boolean)