From: Mike Bayer Date: Sun, 30 Oct 2022 13:05:05 +0000 (-0400) Subject: add Mapped to attrs example X-Git-Tag: rel_2_0_0b3~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f21f84d47d708211ee32b1ad2ca70bf9fa2d8e96;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git add Mapped to attrs example this doesn't work if we dont include this, or the `__allow_unmapped__` thing, but `Mapped[]` is nicer Change-Id: Iccdeb865d4e615ea6740720f6656e2558a2b36b8 References: #8740 --- diff --git a/doc/build/orm/dataclasses.rst b/doc/build/orm/dataclasses.rst index 8a18c9461f..a0ac0e3cb5 100644 --- a/doc/build/orm/dataclasses.rst +++ b/doc/build/orm/dataclasses.rst @@ -761,10 +761,10 @@ object is declared inline with the declarative class. The ``@define`` decorator is applied to the class first, then the :meth:`_orm.registry.mapped` decorator second:: - from __future__ import annotations from typing import List + from typing import Optional from attrs import define from sqlalchemy import Column @@ -773,6 +773,7 @@ object is declared inline with the declarative class. The from sqlalchemy import MetaData from sqlalchemy import String from sqlalchemy import Table + from sqlalchemy.orm import Mapped from sqlalchemy.orm import registry from sqlalchemy.orm import relationship @@ -787,14 +788,14 @@ object is declared inline with the declarative class. The mapper_registry.metadata, Column("id", Integer, primary_key=True), Column("name", String(50)), - Column("fullname", String(50)), + Column("FullName", String(50), key="fullname"), Column("nickname", String(12)), ) - id: int - name: str - fullname: str - nickname: str - addresses: List[Address] + id: Mapped[int] + name: Mapped[str] + fullname: Mapped[str] + nickname: Mapped[str] + addresses: Mapped[List[Address]] __mapper_args__ = { # type: ignore "properties": { @@ -813,9 +814,9 @@ object is declared inline with the declarative class. The Column("user_id", Integer, ForeignKey("user.id")), Column("email_address", String(50)), ) - id: int - user_id: int - email_address: Optional[str] + id: Mapped[int] + user_id: Mapped[int] + email_address: Mapped[Optional[str]] .. note:: The ``attrs`` ``slots=True`` option, which enables ``__slots__`` on a mapped class, cannot be used with SQLAlchemy mappings without fully