Skip to content

Commit 68b31d7

Browse files
committed
Simplify alias propagation logic for Pydantic v2
1 parent 7b2bc2c commit 68b31d7

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

sqlmodel/_compat.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,7 @@ def get_field_metadata(field: Any) -> Any:
235235
return FakeMetadata()
236236

237237
def post_init_field_info(field_info: FieldInfo) -> None:
238-
if field_info.alias and not field_info.validation_alias:
239-
field_info.validation_alias = field_info.alias
240-
if field_info.alias and not field_info.serialization_alias:
241-
field_info.serialization_alias = field_info.alias
238+
return None
242239

243240
# Dummy to make it importable
244241
def _calculate_keys(

sqlmodel/main.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -437,21 +437,13 @@ def Field(
437437
**current_schema_extra,
438438
}
439439
if IS_PYDANTIC_V2:
440-
# explicit params > schema_extra > alias propagation (handled later)
441-
effective_validation_alias = (
442-
validation_alias
443-
if validation_alias is not None
444-
else schema_validation_alias
440+
# explicit params > schema_extra > alias propagation
441+
field_info_kwargs["validation_alias"] = (
442+
validation_alias or schema_validation_alias or alias
445443
)
446-
effective_serialization_alias = (
447-
serialization_alias
448-
if serialization_alias is not None
449-
else schema_serialization_alias
444+
field_info_kwargs["serialization_alias"] = (
445+
serialization_alias or schema_serialization_alias or alias
450446
)
451-
if effective_validation_alias is not None:
452-
field_info_kwargs["validation_alias"] = effective_validation_alias
453-
if effective_serialization_alias is not None:
454-
field_info_kwargs["serialization_alias"] = effective_serialization_alias
455447
else:
456448
if validation_alias or schema_validation_alias is not None:
457449
raise RuntimeError("validation_alias is not supported in Pydantic v1")

0 commit comments

Comments
 (0)