From 22b3308b1c6d2f5261ac13d19e9ac95eb62b36b6 Mon Sep 17 00:00:00 2001 From: mUniKeS Date: Fri, 16 Feb 2024 15:41:21 +0100 Subject: [PATCH] [Bug] Fix: pydantic.errors.PydanticUserError: Field 'id' requires a type annotation, when create from sqlalchemy 2.0 model, without pydantic model --- fastapi_amis_admin/crud/parser.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fastapi_amis_admin/crud/parser.py b/fastapi_amis_admin/crud/parser.py index 243b609..a771c68 100644 --- a/fastapi_amis_admin/crud/parser.py +++ b/fastapi_amis_admin/crud/parser.py @@ -348,15 +348,17 @@ def insfield_to_modelfield(insfield: InstrumentedAttribute) -> Optional[ModelFie elif expression.default.is_callable: field_info_kwargs["default_factory"] = expression.default.arg required = False - if isinstance(expression.type, String): + if isinstance(expression.type, String) and expression.type.length: field_info_kwargs["max_length"] = expression.type.length - if "default_factory" not in field_info_kwargs: + if "default_factory" not in field_info_kwargs and default: field_info_kwargs["default"] = default type_ = expression.type.python_type if PYDANTIC_V2: field_info_kwargs["annotation"] = type_ + if expression.comment: + field_info_kwargs["title"] = expression.comment return create_response_field( - name=insfield.key, type_=type_, required=required, field_info=Field(title=expression.comment, **field_info_kwargs) + name=insfield.key, type_=type_, required=required, field_info=FieldInfo(**field_info_kwargs) )