Skip to content
Advertisement

SQLAlchemy Column Types As Generic (BIT Type)

I am trying to list out the columns of each table in an MSSQL database. I can get them fine, but I need to turn them into generic types for use elsewhere. (Python types would be more ideal but not sure how to do that?)

My code so far works until I come across a BIT type column.

from sqlalchemy import *
from sqlalchemy.engine import URL

connection_string = f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={auth['server']};DATABASE={auth['database']};UID={auth['username']};PWD={auth['password']}"
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})

engine = create_engine(connection_url)

metadata = MetaData(schema="Audit")

metadata.reflect(bind=engine)

for table in metadata.sorted_tables:
    print('n' + table.name)
    for col in table.columns:
        name = col.name
        type = col.type.as_generic()
        print(name, type)

I get the error:

NotImplementedError: Default TypeEngine.as_generic() heuristic method was unsuccessful for sqlalchemy.dialects.mssql.base.BIT. A custom as_generic() method must be implemented for this type class.

I have tried a number of things to work around this, but I’d like to learn what I need to do to fix it properly.

Can I make a custom method that turns BIT to INTEGER? And if so how can I implement it?

Thanks,

Advertisement

Answer

Solved. Was a bug, see comments on first post.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement