Created
January 15, 2022 15:38
-
-
Save marksparrish/354619f12e13d20aaf60b6c6df016df5 to your computer and use it in GitHub Desktop.
Get Table Column using SQLAlcemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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