1

I am trying to generate documentation for a database created with sqlalchemy, and want to include column SQL data types, which are dependent on the current engine. Is there any way to extract this from sqlalchemy? I tried following the metadata.create_all(engine) rabbit hole but quickly got lost.

Essentially what I'm after is a mapping:

{
    'Integer': 'INT',
    'String': 'VARCHAR',
    ...
}

which is specific to the current engine and takes a sqlalchemy type to a SQL data type.

2
  • Do you mean you're using the generic types and want to know what they actually compile to? Commented Jun 26, 2018 at 9:36
  • Yep, I've updated the question to make it clearer. Commented Jun 26, 2018 at 10:34

1 Answer 1

3

You can create/use a dialect instance and its type compiler:

In [18]: engine = create_engine('postgresql://sdf:sdf@localhost/sdf')

In [19]: types.Unicode().compile(dialect=engine.dialect)
Out[19]: 'VARCHAR'

Note that you can create relevant Dialect subclass instances without an engine as well:

In [20]: from sqlalchemy.dialects import mssql

In [21]: d = mssql.dialect()

In [22]: types.Unicode(32).compile(dialect=d)
Out[22]: 'NVARCHAR(32)'
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! This is both exactly what I was after and far simpler than I'd hoped. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.