Skip to content

Commit eb3cfcc

Browse files
committed
Fix field annotation retrieval for pydantic integration
1 parent ee8fd7b commit eb3cfcc

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

sqlmodel/main.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -572,18 +572,18 @@ def get_config(name: str) -> Any:
572572
if isinstance(original_field, FieldInfo):
573573
field = original_field
574574
else:
575-
annotated_field = next(
576-
ann
577-
for c in new_cls.__mro__
578-
if (ann := get_annotations(c.__dict__).get(k)) # type: ignore[arg-type]
579-
)
580-
annotated_field_meta = getattr(
581-
annotated_field, "__metadata__", (())
582-
)
583-
field = next(
584-
(f for f in annotated_field_meta if isinstance(f, FieldInfo)),
585-
v,
586-
) # type: ignore[assignment]
575+
field = v # type: ignore[assignment]
576+
# Update the FieldInfo with the correct class from annotation.
577+
# This is required because pydantic overrides the field with its own
578+
# class and the reference for sqlmodel.FieldInfo is lost.
579+
for c in new_cls.__mro__:
580+
if annotated := get_annotations(c.__dict__).get(k): # type: ignore[arg-type]
581+
for meta in getattr(annotated, "__metadata__", ()):
582+
if isinstance(meta, FieldInfo):
583+
field = meta
584+
break
585+
break
586+
587587
field.annotation = v.annotation
588588
# Guarantee the field has the correct type
589589
col = get_column_from_field(field)

0 commit comments

Comments
 (0)