Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy Boolean vs BOOLEAN

Tags:

sqlalchemy

I can see that BOOLEAN overrides __visit_name__

class BOOLEAN(Boolean):
    __visit_name__ = 'BOOLEAN'

that controls visitor's method chosen by the dispatcher

def _compiler_dispatch(self, visitor, **kw):
    visit_attr = 'visit_%s' % self.__visit_name__
    try:
        meth = getattr(visitor, visit_attr)

In the case of MS SQL, the visitor is MSSQLCompiler, which derives from GenericTypeCompiler. Now, MSSQLCompiler overrides visit_boolean:

def visit_boolean(self, type_, **kw):
    return self.visit_BIT(type_)

But it does not override visit_BOOLEAN, so column type Boolean resolves to "BIT", and BOOLEAN (via the superclass) resolves to "BOOLEAN", which is not a valid MS SQL type.

Why this inconsistency? Does MSSQLCompiler need to override visit_BOOLEAN, or is there something I am missing? What is the difference, conceptually, between Boolean and BOOLEAN types?

like image 413
user443854 Avatar asked Oct 27 '25 05:10

user443854


1 Answers

Boolean is a generic type:

Generic types specify a column that can read, write and store a particular type of Python data. SQLAlchemy will choose the best database column type available on the target database when issuing a CREATE TABLE statement.

BOOLEAN is a SQL type:

This category of types refers to types that are either part of the SQL standard, or are potentially found within a subset of database backends. Unlike the “generic” types, the SQL standard/multi-vendor types have no guarantee of working on all backends, and will only work on those backends that explicitly support them by name. That is, the type will always emit its exact name in DDL with CREATE TABLE is issued.

There's no inconsistency, because they're different things.

like image 118
univerio Avatar answered Oct 30 '25 16:10

univerio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!