pydantic-sqlalchemy
pydantic-sqlalchemy copied to clipboard
Finding Python types for Columns
Hi, thanks for this function just as I started putting together one of my own. Regarding this code:
https://github.com/tiangolo/pydantic-sqlalchemy/blob/8667e213a6ad8e62288fad2a6621958e8ab0b688/pydantic_sqlalchemy/main.py#L24-L30
Have you looked at the typing.get_type_hints() function? Perhaps
else:
python_types = typing.get_type_hints(db_model)
python_type = python_types[column.name]
works for you as well?
from sqlalchemy.sql import sqltypes import typing
...
def python_type_for_column(db_model, column): # Check if the column type is in SQLAlchemy's type map if isinstance(column.type, sqltypes.TypeEngine): python_type = column.type.python_type else: python_types = typing.get_type_hints(db_model) python_type = python_types[column.name] return python_type