Problem with the metadata field (NameError: Field name "metadata" shadows a BaseModel attribute; use a different field name with "alias='metadata'".) · Issue #290 · fastapi/sqlmodel · GitHub
Problem with the metadata field (NameError: Field name "metadata" shadows a BaseModel attribute; use a different field name with "alias='metadata'".) #290
I created a model but this have a field named metadata, is an existing database, so I can't change the fields names, since the SQLModel class also has a field called metadata, when I run the app I get the following error
NameError: Field name "metadata" shadows a BaseModel attribute; use a different field name with "alias='metadata'".
I used the Field class of sqlmodel and assigned the alias but it does not work.
metadata_: Optional[str] = Field(default=None, alias="metadata")
With this the error is that Table.metadata_ field does not exist
Is there any way to create an tabla with the metadata field without giving problems with SQLModel?
I have found the solution using the sa_column argument for the Field class, but the alias argument in the SQLModel class should work.
example code.
metadata_: Optional[str] = Field(sa_column=Column("metadata", String, default=None))
Reacted by Marcelo Huerta, ChouUn, Jonas, Didi Ruhyadi, x.hw, Alex Kosh, Tang Ziya, Philip Nicolcev and Thomas Gardner
e=1 validation error for SQLModelWithSQLModelField
metadata_
Field required [type=missing, input_value={'metadata': 'metadata'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.8/v/missing
sql_model_with_suffix.model_dump(by_alias=False)={'metadata_': 'metadata'}
sql_model_with_suffix.model_dump(by_alias=True)={'metadata_': 'metadata'}
sql_model_with_pydantic_field.model_dump(by_alias=False)={'metadata_': 'metadata'}
sql_model_with_pydantic_field.model_dump(by_alias=True)={'metadata': 'metadata'}
e=1 validation error for SQLModelWithPydanticField
metadata
Field required [type=missing, input_value={'metadata_': 'metadata'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.8/v/missing
pydantic_model_with_pydantic_field.model_dump(by_alias=False)={'metadata_': 'metadata'}
pydantic_model_with_pydantic_field.model_dump(by_alias=True)={'metadata': 'metadata'}
e=1 validation error for PydanticModelWithPydanticField
metadata
Field required [type=missing, input_value={'metadata_': 'metadata'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.8/v/missing
e=1 validation error for PydanticModelWithSQLModelField
metadata_
Field required [type=missing, input_value={'metadata': 'metadata'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.8/v/missing
pydantic_model_with_sql_model_field_with_suffix.model_dump(by_alias=False)={'metadata_': 'metadata'}
pydantic_model_with_sql_model_field_with_suffix.model_dump(by_alias=True)={'metadata_': 'metadata'}
Current walk around
Define a new response model inheirt from pydantic.BaseModel instead of SQLModel for fastapi.
So, current solution is to use Field(sa_column=Column("metadata", ...).
I personally think it would be good to use alias parameter as a default value for column name.
I just created an issue to track problems with alias parameter and mentioned this idea there.
I'll close this issue now and let's track all alias-related things in that new issue
Activity
MichaelLan commented on Mar 29, 2022
I have found the solution using the sa_column argument for the Field class, but the alias argument in the SQLModel class should work.
example code.
metadata_: Optional[str] = Field(sa_column=Column("metadata", String, default=None))
tcztzy commented on Aug 1, 2024
Would this problem be fixed? Do we have any walkaround for metadata's validation/serialization currently?
tcztzy commented on Aug 1, 2024
Some research
output of previous code
e=1 validation error for SQLModelWithSQLModelField metadata_ Field required [type=missing, input_value={'metadata': 'metadata'}, input_type=dict] For further information visit https://errors.pydantic.dev/2.8/v/missing sql_model_with_suffix.model_dump(by_alias=False)={'metadata_': 'metadata'} sql_model_with_suffix.model_dump(by_alias=True)={'metadata_': 'metadata'} sql_model_with_pydantic_field.model_dump(by_alias=False)={'metadata_': 'metadata'} sql_model_with_pydantic_field.model_dump(by_alias=True)={'metadata': 'metadata'} e=1 validation error for SQLModelWithPydanticField metadata Field required [type=missing, input_value={'metadata_': 'metadata'}, input_type=dict] For further information visit https://errors.pydantic.dev/2.8/v/missing pydantic_model_with_pydantic_field.model_dump(by_alias=False)={'metadata_': 'metadata'} pydantic_model_with_pydantic_field.model_dump(by_alias=True)={'metadata': 'metadata'} e=1 validation error for PydanticModelWithPydanticField metadata Field required [type=missing, input_value={'metadata_': 'metadata'}, input_type=dict] For further information visit https://errors.pydantic.dev/2.8/v/missing e=1 validation error for PydanticModelWithSQLModelField metadata_ Field required [type=missing, input_value={'metadata': 'metadata'}, input_type=dict] For further information visit https://errors.pydantic.dev/2.8/v/missing pydantic_model_with_sql_model_field_with_suffix.model_dump(by_alias=False)={'metadata_': 'metadata'} pydantic_model_with_sql_model_field_with_suffix.model_dump(by_alias=True)={'metadata_': 'metadata'}Current walk around
Define a new response model inheirt from pydantic.BaseModel instead of SQLModel for fastapi.
conclusion
This problem seems come from SQLModel's Field
IEH-intekglobal commented on Jan 22, 2025
the serialization_alias needs to be set for the FieldInfo. This can be done through the schema_extra argument
YuriiMotov commented on Aug 26, 2025
So, current solution is to use Field(sa_column=Column("metadata", ...).
I personally think it would be good to use alias parameter as a default value for column name.
I just created an issue to track problems with alias parameter and mentioned this idea there.
I'll close this issue now and let's track all alias-related things in that new issue