pydantic-sqlalchemy icon indicating copy to clipboard operation
pydantic-sqlalchemy copied to clipboard

Finding Python types for Columns

Open jenstroeger opened this issue 3 years ago • 1 comments

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?

jenstroeger avatar Apr 07 '22 21:04 jenstroeger

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

ljluestc avatar Sep 03 '23 19:09 ljluestc