-
-
Notifications
You must be signed in to change notification settings - Fork 720
Problem with the metadata field (NameError: Field name "metadata" shadows a BaseModel attribute; use a different field name with "alias='metadata'".) #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I have found the solution using the sa_column argument for the Field class, but the alias argument in the SQLModel class should work. |
Would this problem be fixed? Do we have any walkaround for |
Some researchfrom pydantic import BaseModel
from pydantic import Field as PydanticField
from sqlmodel import Field, SQLModel
class PydanticModelWithPydanticField(BaseModel):
metadata_: str = PydanticField(alias="metadata")
class PydanticModelWithSQLModelField(BaseModel):
metadata_: str = Field(alias="metadata")
class SQLModelWithPydanticField(SQLModel):
metadata_: str = PydanticField(alias="metadata")
class SQLModelWithSQLModelField(SQLModel):
metadata_: str = Field(alias="metadata")
if __name__ == "__main__":
try:
sql_model_no_suffix = SQLModelWithSQLModelField(metadata="metadata")
print(f'{sql_model_no_suffix.model_dump(by_alias=False)=}')
print(f'{sql_model_no_suffix.model_dump(by_alias=True)=}')
except Exception as e:
print(f'{e=}')
sql_model_with_suffix = SQLModelWithSQLModelField(metadata_="metadata")
print(f'{sql_model_with_suffix.model_dump(by_alias=False)=}')
print(f'{sql_model_with_suffix.model_dump(by_alias=True)=}')
sql_model_with_pydantic_field = SQLModelWithPydanticField(metadata="metadata")
print(f'{sql_model_with_pydantic_field.model_dump(by_alias=False)=}')
print(f'{sql_model_with_pydantic_field.model_dump(by_alias=True)=}')
try:
sql_model_with_pydantic_field_with_suffix = SQLModelWithPydanticField(metadata_="metadata")
print(f'{sql_model_with_pydantic_field_with_suffix.model_dump(by_alias=False)=}')
print(f'{sql_model_with_pydantic_field_with_suffix.model_dump(by_alias=True)=}')
except Exception as e:
print(f'{e=}')
pydantic_model_with_pydantic_field = PydanticModelWithPydanticField(metadata="metadata")
print(f'{pydantic_model_with_pydantic_field.model_dump(by_alias=False)=}')
print(f'{pydantic_model_with_pydantic_field.model_dump(by_alias=True)=}')
try:
pydantic_model_with_pydantic_field_with_suffix = PydanticModelWithPydanticField(metadata_="metadata")
print(f'{pydantic_model_with_pydantic_field_with_suffix.model_dump(by_alias=False)=}')
print(f'{pydantic_model_with_pydantic_field_with_suffix.model_dump(by_alias=True)=}')
except Exception as e:
print(f'{e=}')
try:
pydantic_model_no_suffix = PydanticModelWithSQLModelField(metadata="metadata")
print(f'{pydantic_model_no_suffix.model_dump(by_alias=False)=}')
print(f'{pydantic_model_no_suffix.model_dump(by_alias=True)=}')
except Exception as e:
print(f'{e=}')
pydantic_model_with_sql_model_field_with_suffix = PydanticModelWithSQLModelField(metadata_="metadata")
print(f'{pydantic_model_with_sql_model_field_with_suffix.model_dump(by_alias=False)=}')
print(f'{pydantic_model_with_sql_model_field_with_suffix.model_dump(by_alias=True)=}') output of previous code
Current walk aroundDefine a new response model inheirt from pydantic.BaseModel instead of SQLModel for fastapi. conclusionThis problem seems come from SQLModel's Field |
the serialization_alias needs to be set for the FieldInfo. This can be done through the schema_extra argument metadata_: dict = Field(sa_column=Column("metadata", JSON, nullable=False), default_factory=dict, schema_extra={'serialization_alias': 'metadata'}) |
First Check
Commit to Help
Example Code
Description
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?
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
Python 3.9.5
Additional Context
No response
The text was updated successfully, but these errors were encountered: