Created
September 30, 2024 20:16
-
-
Save GGontijo/11aead2b403a749303958ef495e32cc8 to your computer and use it in GitHub Desktop.
Equalize Pydantic columns with an un-mapped SQLAlchemy table / Equaliza colunas pydantic com tabela sqlalchemy não mapeada
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Se você não quiser mapear uma tabela do sqlalchemy e esta tendo erros por conta de colunas | |
# no pydantic que não existem em sua tabela, você pode filtra-las desta forma | |
# If you don't want to map an SQLAlchemy table and are getting errors due to columns | |
# in Pydantic that don't exist in your table, you can filter them this way | |
def filtrar_colunas_validas(data, table) -> dict: | |
"""Remove colunas que não são utilizadas na tabela""" | |
valid_columns = set(table.columns.keys()) | |
return {key: value for key, value in data.items() if key in valid_columns} | |
# Usage | |
filtered_data = filtrar_colunas_validas(pydanticobj.model_dump(), example_sqlalchemy_table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment