SQLAlchemy getting column data types of view results in MySQL

Viewed 18

I've seen a few posts on this but nothing that works for me unfortunately.

Basically trying to get the SQLAlchemy (or pandas) column data types from a list of views in a MySQL database.

import sqlalchemy as sa
view = "myView"
engine = "..."
meta = sa.MetaData(engine, True)

This errors:

tb_data = meta.tables["tb_data"]
# KeyError: 'tb_data'

And I don't know what I'm supposed to do with this:

sa.Table(view, meta).columns
# <sqlalchemy.sql.base.ImmutableColumnCollection at 0x7f9cb264d4a0>

Saw this somewhere but not sure how I'm supposed to use it:

str_columns = filter(lambda column: isinstance(column.type, sa.TEXT), columns)
# <filter at 0x7f9caafab640>

Eventually what I'm trying to achieve is a list or dict of data types for a view that I can then use to load to a PostgreSQL database. Happy to consider alternatives outside of sqlalchemy and/or pandas if they exist (and are relatively trivial to implement).

0 Answers
Related