diff --git a/docs/examples/data_transfer_objects/factory/marking_fields.py b/docs/examples/data_transfer_objects/factory/marking_fields.py index 7649f2f8b5..0574273c3f 100644 --- a/docs/examples/data_transfer_objects/factory/marking_fields.py +++ b/docs/examples/data_transfer_objects/factory/marking_fields.py @@ -10,6 +10,8 @@ class User(Base): + # `Base` defines `id` field as: + # id: Mapped[UUID] = mapped_column(default=uuid4, primary_key=True) name: Mapped[str] password: Mapped[str] = mapped_column(info=dto_field("private")) created_at: Mapped[datetime] = mapped_column(info=dto_field("read-only")) @@ -20,6 +22,9 @@ class User(Base): @post("/users", dto=UserDTO, sync_to_thread=False) def create_user(data: User) -> User: + # even though the client did not send the id field, + # since it is a primary key it is autogenerated + assert "id" in vars(data) # even though the client sent the password and created_at field, it is not in the data object assert "password" not in vars(data) assert "created_at" not in vars(data) diff --git a/docs/examples/data_transfer_objects/factory/simple_dto_factory_example.py b/docs/examples/data_transfer_objects/factory/simple_dto_factory_example.py index 0bcf7fd184..88f78beb16 100644 --- a/docs/examples/data_transfer_objects/factory/simple_dto_factory_example.py +++ b/docs/examples/data_transfer_objects/factory/simple_dto_factory_example.py @@ -9,6 +9,8 @@ class User(Base): + # `Base` defines `id` field as: + # id: Mapped[UUID] = mapped_column(default=uuid4, primary_key=True) name: Mapped[str] password: Mapped[str] created_at: Mapped[datetime] diff --git a/docs/usage/dto/1-abstract-dto.rst b/docs/usage/dto/1-abstract-dto.rst index 5c8d8995d9..a39f583ef2 100644 --- a/docs/usage/dto/1-abstract-dto.rst +++ b/docs/usage/dto/1-abstract-dto.rst @@ -50,6 +50,8 @@ Fields marked as ``"private"`` or ``"read-only"`` will not be parsed from client :emphasize-lines: 6,14,15 :linenos: +Note that ``id`` field is the primary key and is handled specially by the defined SQLAlchemy base. + .. note: The procedure for "marking" a model field will vary depending on the library. For example,