Skip to content

Instantly share code, notes, and snippets.

@marksparrish
Created January 15, 2022 15:38
Show Gist options
  • Save marksparrish/354619f12e13d20aaf60b6c6df016df5 to your computer and use it in GitHub Desktop.
Save marksparrish/354619f12e13d20aaf60b6c6df016df5 to your computer and use it in GitHub Desktop.
Get Table Column using SQLAlcemy
from sqlalchemy import MetaData, Table
from models import engines
# engine is expecting a create_engine
def get_table_columns(table_name, engine):
metadata = MetaData()
meta_table_columns = Table(table_name, metadata, autoload_with=engine).columns
table_columns = []
for name in meta_table_columns:
# The columns function returns the columns names with the table names seperated by a .
# I split the column name on dots and take the last element which is just the column name
table_columns.append(str(name).split('.')[len((str(name).split('.')))-1])
return table_columns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment