1

How can I represent this query in SQLAlchemy?

SELECT COLUMN_NAME, DATA_TYPE 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'foo'
3
  • Why you need that with SQLAlchemy ? Did you lost the model ? Commented Jul 25, 2016 at 13:24
  • I want to be able to read the datatypes for columns in a table foo. Commented Jul 25, 2016 at 13:25
  • @polku is it possible to do this? Commented Jul 25, 2016 at 13:51

2 Answers 2

4

Mb, I didn't understood you meant the database types. If it's a mapped table you can iterate columns like this:

for c in foo.__table__.columns:
    print(c.type)
Sign up to request clarification or add additional context in comments.

2 Comments

Just a table on sql server 2008. I just want a function that returns the column names and types.
Then I go back to my first question, you don't really need SQLAlchemy, just open a connection and execute your query (3 lines in Python). You can use SQLA automap, but that's really seems overkill.
0
def get_data_types(model):
'''return dictionary with column name as key and data type as value'''
    d = {}
    for c in model.__table__.columns:
        d[str(c.name)] = str(c.type)

    return d

where model is the sqlalchemy orm table.

Comments

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.