Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Server BIT is native boolean #4061

Closed
sqlalchemy-bot opened this issue Aug 30, 2017 · 5 comments
Closed

SQL Server BIT is native boolean #4061

sqlalchemy-bot opened this issue Aug 30, 2017 · 5 comments
Labels
bug Something isn't working SQL Server Microsoft SQL Server, e.g. mssql
Milestone

Comments

@sqlalchemy-bot
Copy link
Collaborator

Migrated issue, originally created by Michael Bayer (@zzzeek)

we should not be creating a constraint for Boolean on SQL Server and we should support native boolean rules. The BIT type only stores 1 or 0 and converts any other integer into a 1:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class A(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)
    data = Column(Boolean(create_constraint=False))

#e = create_engine("mssql+pymssql://scott:tiger^5HHH@mssql2017:1433/test", echo=True)
e = create_engine("mssql+pyodbc://scott:tiger^5HHH@mssql2017:1433/test?driver=FreeTDS", echo=True)

Base.metadata.drop_all(e)
Base.metadata.create_all(e)

conn = e.connect()
conn.execute("INSERT INTO a (data) VALUES (1)")
conn.execute("INSERT INTO a (data) VALUES (15)")

if e.dialect.driver == 'pymssql':
    conn.execute("INSERT INTO a (data) VALUES (%(data)s)", {"data": True})
    conn.execute("INSERT INTO a (data) VALUES (%(data)s)", {"data": 25})
    conn.execute("INSERT INTO a (data) VALUES (%(data)s)", {"data": 1})
else:
    conn.execute("INSERT INTO a (data) VALUES (?)", [True])
    conn.execute("INSERT INTO a (data) VALUES (?)", [25])
    conn.execute("INSERT INTO a (data) VALUES (?)", [1])


print e.execute(select([A.data])).fetchall()
@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

Enable native boolean for SQL Server

SQL Server supports what SQLAlchemy calls "native boolean"
with its BIT type, as this type only accepts 0 or 1 and the
DBAPIs return its value as True/False. So the SQL Server
dialects now enable "native boolean" support, in that a
CHECK constraint is not generated for a :class:.Boolean
datatype. The only difference vs. other native boolean
is that there are no "true" / "false" constants so "1" and
"0" are still rendered here.

Tests are implicit in the existing suites.

Change-Id: I75bbcd549884099fb1a177e68667bf880c40fa7c
Fixes: #4061

888f112

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

regression. SQL Server does not allow "WHERE 1", see #4250. we need a third state of boolean that is in between "native" and "numeric"

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

OK also, the drivers do seem to be returning True/False for this type natively, so we technically don't need the result processor either.

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

but....int_to_bool is not a big deal. not worth complicating it more.

@sqlalchemy-bot sqlalchemy-bot added SQL Server Microsoft SQL Server, e.g. mssql bug Something isn't working labels Nov 27, 2018
@sqlalchemy-bot sqlalchemy-bot added this to the 1.2 milestone Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working SQL Server Microsoft SQL Server, e.g. mssql
Projects
None yet
Development

No branches or pull requests

1 participant