0

By doing this :

 columns = [[column.name, column.type] for column in inspect(table).c]
 print(columns)

I managed to get the name and the type of a column. But the type I get is on the sqlalchemy format, not in python format. I would also like to know if the table is nullable.

For example, if we have this column :

 username = Column(String(45), nullable=False)

I would like to get something like :

 ["username", str, 45, False]
asked Jan 27, 2022 at 23:00
2
  • You need to type map it. you have to write custom code for that. Commented Jan 27, 2022 at 23:51
  • @farhanpirzadaa Ok but how though ? I dont understand how to do it. Most of the tutorials of mapping talks about declarative table and default values. Commented Jan 28, 2022 at 8:58

1 Answer 1

1

You can get the Python type from the column's type's python_type attribute, length from the type, nullable from the column:

>>> username = sa.Column(sa.String(45), nullable=False)
>>> username.type.python_type
<class 'str'>
>>> username.type.length
45
>>> username.nullable
False
answered Jan 28, 2022 at 10:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.