0

The (I think) simple question - how can I use SQL Alchemy automap extension to list the columns of a table within a schema - importantly - of an existing mysql database??

This is what I have been attempting (trying to follow docs here: Automap):

metadata = MetaData()
metadata.reflect(engine)

Base = automap_base(metadata=metadata)
Base.prepare()

From this point, I can 'see' all the tables (e.g. Base.classes.table1, Base.classes.table2 etc), and can 'see' the columns in a particular table as attributes of each table (e.g. Base.classes.table1.column1, Base.classes.column2).

The question is basically is there a simple way to list the columns in the tables? (or indeed the tables in the schema?).. Something analagous to SHOW COLUMNS IN 'table1'... Maybe I am missing somethings obvious / basic here.

A perhaps related comment - I have been both a mysql admin/user and a python user for several years now, and I am only just dipping my toes into the ORM world. Maybe I am just slow, but I am finding the docs for SQL Alchemy to be pretty impenetrable - there is seems to be mountains and mountains of documentation (and whole new vocabularly to use)... or maybe I am just thinking about ORM in completely the wrong way - so any basic guides etc - that aren't from SQL Alchemy - would be appreciated. Perhaps something along the lines of "SQL Alchemy for SQL users" - if such a thing exists..

djmac
  • 827
  • 5
  • 11
  • 27
  • To your related comment, SQLAlchemy is a huge library, so it follows that the documentation is vast. Try starting from the basics, like the [Object Relational Tutorial](http://docs.sqlalchemy.org/en/latest/orm/tutorial.html), teaching yourself a useful subset first. – Ilja Everilä Jul 04 '17 at 09:52
  • Note that what you see are not tables, but the automatically generated mapped classes, which the attribute `Base.classes` hints at. – Ilja Everilä Jul 04 '17 at 10:01

1 Answers1

2

The answer is basically contained in the question link to by Ilja Everilä. Will mark as duplicate - but here is the direct answer to my question:

Base.classes.table1.__table__.columns will provide you with columns information (as an SQL Alchemy ImmutableColumnCollection object, which has a handful of methods).

One of those methods will yield a list of column names, specifically:

Base.classes.table1.__table__.columns.keys()

...Basically the main thing I was missing was the "magic" __table__ attribute/object.

djmac
  • 827
  • 5
  • 11
  • 27