diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e6da15389..597cc9801 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,7 +12,7 @@ Changelog ------ Fixed ^^^^^ -- TODO +- Primary key field should not be nullable (#1778) Added ^^^^^ diff --git a/tortoise/contrib/pydantic/creator.py b/tortoise/contrib/pydantic/creator.py index 32ff88ffd..6f2585fe5 100644 --- a/tortoise/contrib/pydantic/creator.py +++ b/tortoise/contrib/pydantic/creator.py @@ -513,7 +513,9 @@ def _process_data_field( ptype = python_type if field.null: json_schema_extra["nullable"] = True - if field_name in self._optional or field.default is not None or field.null: + if not field.pk and ( + field_name in self._optional or field.default is not None or field.null + ): ptype = Optional[ptype] if not (self._exclude_read_only and json_schema_extra.get("readOnly") is True): return annotation or ptype