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
-
You need to type map it. you have to write custom code for that.farhan pirzada– farhan pirzada2022年01月27日 23:51:47 +00:00Commented 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.Barthélémy Déchanet– Barthélémy Déchanet2022年01月28日 08:58:47 +00:00Commented Jan 28, 2022 at 8:58
1 Answer 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
lang-py