File: composites.rst

package info (click to toggle)
sqlalchemy 2.0.40%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 26,404 kB
  • sloc: python: 410,002; makefile: 230; sh: 7
file content (480 lines) | stat: -rw-r--r-- 16,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
.. currentmodule:: sqlalchemy.orm

.. _mapper_composite:

Composite Column Types
======================

Sets of columns can be associated with a single user-defined datatype,
which in modern use is normally a Python dataclass_. The ORM
provides a single attribute which represents the group of columns using the
class you provide.

A simple example represents pairs of :class:`_types.Integer` columns as a
``Point`` object, with attributes ``.x`` and ``.y``.   Using a
dataclass, these attributes are defined with the corresponding ``int``
Python type::

    import dataclasses


    @dataclasses.dataclass
    class Point:
        x: int
        y: int

Non-dataclass forms are also accepted, but require additional methods
to be implemented.  For an example using a non-dataclass class, see the section
:ref:`composite_legacy_no_dataclass`.

.. versionadded:: 2.0 The :func:`_orm.composite` construct fully supports
   Python dataclasses including the ability to derive mapped column datatypes
   from the composite class.

We will create a mapping to a table ``vertices``, which represents two points
as ``x1/y1`` and ``x2/y2``.   The ``Point`` class is associated with
the mapped columns using the :func:`_orm.composite` construct.

The example below illustrates the most modern form of :func:`_orm.composite` as
used with a fully
:ref:`Annotated Declarative Table <orm_declarative_mapped_column>`
configuration. :func:`_orm.mapped_column` constructs representing each column
are passed directly to :func:`_orm.composite`, indicating zero or more aspects
of the columns to be generated, in this case the names; the
:func:`_orm.composite` construct derives the column types (in this case
``int``, corresponding to :class:`_types.Integer`) from the dataclass directly::

    from sqlalchemy.orm import DeclarativeBase, Mapped
    from sqlalchemy.orm import composite, mapped_column


    class Base(DeclarativeBase):
        pass


    class Vertex(Base):
        __tablename__ = "vertices"

        id: Mapped[int] = mapped_column(primary_key=True)

        start: Mapped[Point] = composite(mapped_column("x1"), mapped_column("y1"))
        end: Mapped[Point] = composite(mapped_column("x2"), mapped_column("y2"))

        def __repr__(self):
            return f"Vertex(start={self.start}, end={self.end})"

.. tip:: In the example above the columns that represent the composites
    (``x1``, ``y1``, etc.) are also accessible on the class but are not
    correctly understood by type checkers.
    If accessing the single columns is important they can be explicitly declared,
    as shown in :ref:`composite_with_typing`.

The above mapping would correspond to a CREATE TABLE statement as:

.. sourcecode:: pycon+sql

    >>> from sqlalchemy.schema import CreateTable
    >>> print(CreateTable(Vertex.__table__))
    {printsql}CREATE TABLE vertices (
      id INTEGER NOT NULL,
      x1 INTEGER NOT NULL,
      y1 INTEGER NOT NULL,
      x2 INTEGER NOT NULL,
      y2 INTEGER NOT NULL,
      PRIMARY KEY (id)
    )


Working with Mapped Composite Column Types
-------------------------------------------

With a mapping as illustrated in the top section, we can work with the
``Vertex`` class, where the ``.start`` and ``.end`` attributes will
transparently refer to the columns referenced by the ``Point`` class, as
well as with instances of the ``Vertex`` class, where the ``.start`` and
``.end`` attributes will refer to instances of the ``Point`` class. The ``x1``,
``y1``, ``x2``, and ``y2`` columns are handled transparently:

* **Persisting Point objects**

  We can create a ``Vertex`` object, assign ``Point`` objects as members,
  and they will be persisted as expected:

  .. sourcecode:: pycon+sql

    >>> v = Vertex(start=Point(3, 4), end=Point(5, 6))
    >>> session.add(v)
    >>> session.commit()
    {execsql}BEGIN (implicit)
    INSERT INTO vertices (x1, y1, x2, y2) VALUES (?, ?, ?, ?)
    [generated in ...] (3, 4, 5, 6)
    COMMIT

* **Selecting Point objects as columns**

  :func:`_orm.composite` will allow the ``Vertex.start`` and ``Vertex.end``
  attributes to behave like a single SQL expression to as much an extent
  as possible when using the ORM :class:`_orm.Session` (including the legacy
  :class:`_orm.Query` object) to select ``Point`` objects:

  .. sourcecode:: pycon+sql

    >>> stmt = select(Vertex.start, Vertex.end)
    >>> session.execute(stmt).all()
    {execsql}SELECT vertices.x1, vertices.y1, vertices.x2, vertices.y2
    FROM vertices
    [...] ()
    {stop}[(Point(x=3, y=4), Point(x=5, y=6))]

* **Comparing Point objects in SQL expressions**

  The ``Vertex.start`` and ``Vertex.end`` attributes may be used in
  WHERE criteria and similar, using ad-hoc ``Point`` objects for comparisons:

  .. sourcecode:: pycon+sql

    >>> stmt = select(Vertex).where(Vertex.start == Point(3, 4)).where(Vertex.end < Point(7, 8))
    >>> session.scalars(stmt).all()
    {execsql}SELECT vertices.id, vertices.x1, vertices.y1, vertices.x2, vertices.y2
    FROM vertices
    WHERE vertices.x1 = ? AND vertices.y1 = ? AND vertices.x2 < ? AND vertices.y2 < ?
    [...] (3, 4, 7, 8)
    {stop}[Vertex(Point(x=3, y=4), Point(x=5, y=6))]

  .. versionadded:: 2.0  :func:`_orm.composite` constructs now support
     "ordering" comparisons such as ``<``, ``>=``, and similar, in addition
     to the already-present support for ``==``, ``!=``.

  .. tip::  The "ordering" comparison above using the "less than" operator (``<``)
     as well as the "equality" comparison using ``==``, when used to generate
     SQL expressions, are implemented by the :class:`_orm.Composite.Comparator`
     class, and don't make use of the comparison methods on the composite class
     itself, e.g. the ``__lt__()`` or ``__eq__()`` methods. From this it
     follows that the ``Point`` dataclass above also need not implement the
     dataclasses ``order=True`` parameter for the above SQL operations to work.
     The section :ref:`composite_operations` contains background on how
     to customize the comparison operations.

* **Updating Point objects on Vertex Instances**

  By default, the ``Point`` object **must be replaced by a new object** for
  changes to be detected:

  .. sourcecode:: pycon+sql

    >>> v1 = session.scalars(select(Vertex)).one()
    {execsql}SELECT vertices.id, vertices.x1, vertices.y1, vertices.x2, vertices.y2
    FROM vertices
    [...] ()
    {stop}

    >>> v1.end = Point(x=10, y=14)
    >>> session.commit()
    {execsql}UPDATE vertices SET x2=?, y2=? WHERE vertices.id = ?
    [...] (10, 14, 1)
    COMMIT

  In order to allow in place changes on the composite object, the
  :ref:`mutable_toplevel` extension must be used.  See the section
  :ref:`mutable_composites` for examples.



.. _orm_composite_other_forms:

Other mapping forms for composites
----------------------------------

The :func:`_orm.composite` construct may be passed the relevant columns
using a :func:`_orm.mapped_column` construct, a :class:`_schema.Column`,
or the string name of an existing mapped column.   The following examples
illustrate an equivalent mapping as that of the main section above.

Map columns directly, then pass to composite
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here we pass the existing :func:`_orm.mapped_column` instances to the
:func:`_orm.composite` construct, as in the non-annotated example below
where we also pass the ``Point`` class as the first argument to
:func:`_orm.composite`::

    from sqlalchemy import Integer
    from sqlalchemy.orm import mapped_column, composite


    class Vertex(Base):
        __tablename__ = "vertices"

        id = mapped_column(Integer, primary_key=True)
        x1 = mapped_column(Integer)
        y1 = mapped_column(Integer)
        x2 = mapped_column(Integer)
        y2 = mapped_column(Integer)

        start = composite(Point, x1, y1)
        end = composite(Point, x2, y2)

.. _composite_with_typing:

Map columns directly, pass attribute names to composite
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We can write the same example above using more annotated forms where we have
the option to pass attribute names to :func:`_orm.composite` instead of
full column constructs::

    from sqlalchemy.orm import mapped_column, composite, Mapped


    class Vertex(Base):
        __tablename__ = "vertices"

        id: Mapped[int] = mapped_column(primary_key=True)
        x1: Mapped[int]
        y1: Mapped[int]
        x2: Mapped[int]
        y2: Mapped[int]

        start: Mapped[Point] = composite("x1", "y1")
        end: Mapped[Point] = composite("x2", "y2")

Imperative mapping and imperative table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

When using :ref:`imperative table <orm_imperative_table_configuration>` or
fully :ref:`imperative <orm_imperative_mapping>` mappings, we have access
to :class:`_schema.Column` objects directly.  These may be passed to
:func:`_orm.composite` as well, as in the imperative example below::

     mapper_registry.map_imperatively(
         Vertex,
         vertices_table,
         properties={
             "start": composite(Point, vertices_table.c.x1, vertices_table.c.y1),
             "end": composite(Point, vertices_table.c.x2, vertices_table.c.y2),
         },
     )

.. _composite_legacy_no_dataclass:

Using Legacy Non-Dataclasses
----------------------------


If not using a dataclass, the requirements for the custom datatype class are
that it have a constructor
which accepts positional arguments corresponding to its column format, and
also provides a method ``__composite_values__()`` which returns the state of
the object as a list or tuple, in order of its column-based attributes. It
also should supply adequate ``__eq__()`` and ``__ne__()`` methods which test
the equality of two instances.

To illustrate the equivalent ``Point`` class from the main section
not using a dataclass::

    class Point:
        def __init__(self, x, y):
            self.x = x
            self.y = y

        def __composite_values__(self):
            return self.x, self.y

        def __repr__(self):
            return f"Point(x={self.x!r}, y={self.y!r})"

        def __eq__(self, other):
            return isinstance(other, Point) and other.x == self.x and other.y == self.y

        def __ne__(self, other):
            return not self.__eq__(other)

Usage with :func:`_orm.composite` then proceeds where the columns to be
associated with the ``Point`` class must also be declared with explicit
types, using one of the forms at :ref:`orm_composite_other_forms`.


Tracking In-Place Mutations on Composites
-----------------------------------------

In-place changes to an existing composite value are
not tracked automatically.  Instead, the composite class needs to provide
events to its parent object explicitly.   This task is largely automated
via the usage of the :class:`.MutableComposite` mixin, which uses events
to associate each user-defined composite object with all parent associations.
Please see the example in :ref:`mutable_composites`.

.. _composite_operations:

Redefining Comparison Operations for Composites
-----------------------------------------------

The "equals" comparison operation by default produces an AND of all
corresponding columns equated to one another. This can be changed using
the ``comparator_factory`` argument to :func:`.composite`, where we
specify a custom :class:`.CompositeProperty.Comparator` class
to define existing or new operations.
Below we illustrate the "greater than" operator, implementing
the same expression that the base "greater than" does::

    import dataclasses

    from sqlalchemy.orm import composite
    from sqlalchemy.orm import CompositeProperty
    from sqlalchemy.orm import DeclarativeBase
    from sqlalchemy.orm import Mapped
    from sqlalchemy.orm import mapped_column
    from sqlalchemy.sql import and_


    @dataclasses.dataclass
    class Point:
        x: int
        y: int


    class PointComparator(CompositeProperty.Comparator):
        def __gt__(self, other):
            """redefine the 'greater than' operation"""

            return and_(
                *[
                    a > b
                    for a, b in zip(
                        self.__clause_element__().clauses,
                        dataclasses.astuple(other),
                    )
                ]
            )


    class Base(DeclarativeBase):
        pass


    class Vertex(Base):
        __tablename__ = "vertices"

        id: Mapped[int] = mapped_column(primary_key=True)

        start: Mapped[Point] = composite(
            mapped_column("x1"), mapped_column("y1"), comparator_factory=PointComparator
        )
        end: Mapped[Point] = composite(
            mapped_column("x2"), mapped_column("y2"), comparator_factory=PointComparator
        )

Since ``Point`` is a dataclass, we may make use of
``dataclasses.astuple()`` to get a tuple form of ``Point`` instances.

The custom comparator then returns the appropriate SQL expression:

.. sourcecode:: pycon+sql

  >>> print(Vertex.start > Point(5, 6))
  {printsql}vertices.x1 > :x1_1 AND vertices.y1 > :y1_1


Nesting Composites
-------------------

Composite objects can be defined to work in simple nested schemes, by
redefining behaviors within the composite class to work as desired, then
mapping the composite class to the full length of individual columns normally.
This requires that additional methods to move between the "nested" and
"flat" forms are defined.

Below we reorganize the ``Vertex`` class to itself be a composite object which
refers to ``Point`` objects. ``Vertex`` and ``Point`` can be dataclasses,
however we will add a custom construction method to ``Vertex`` that can be used
to create new ``Vertex`` objects given four column values, which will will
arbitrarily name ``_generate()`` and define as a classmethod so that we can
make new ``Vertex`` objects by passing values to the ``Vertex._generate()``
method.

We will also implement the ``__composite_values__()`` method, which is a fixed
name recognized by the :func:`_orm.composite` construct (introduced previously
at :ref:`composite_legacy_no_dataclass`) that indicates a standard way of
receiving the object as a flat tuple of column values, which in this case will
supersede the usual dataclass-oriented methodology.

With our custom ``_generate()`` constructor and
``__composite_values__()`` serializer method, we can now move between
a flat tuple of columns and ``Vertex`` objects that contain ``Point``
instances.   The ``Vertex._generate`` method is passed as the
first argument to the :func:`_orm.composite` construct as the source of new
``Vertex`` instances, and the ``__composite_values__()`` method will be
used implicitly by :func:`_orm.composite`.

For the purposes of the example, the ``Vertex`` composite is then mapped to a
class called ``HasVertex``, which is where the :class:`.Table` containing the
four source columns ultimately resides::

    from __future__ import annotations

    import dataclasses
    from typing import Any
    from typing import Tuple

    from sqlalchemy.orm import composite
    from sqlalchemy.orm import DeclarativeBase
    from sqlalchemy.orm import Mapped
    from sqlalchemy.orm import mapped_column


    @dataclasses.dataclass
    class Point:
        x: int
        y: int


    @dataclasses.dataclass
    class Vertex:
        start: Point
        end: Point

        @classmethod
        def _generate(cls, x1: int, y1: int, x2: int, y2: int) -> Vertex:
            """generate a Vertex from a row"""
            return Vertex(Point(x1, y1), Point(x2, y2))

        def __composite_values__(self) -> Tuple[Any, ...]:
            """generate a row from a Vertex"""
            return dataclasses.astuple(self.start) + dataclasses.astuple(self.end)


    class Base(DeclarativeBase):
        pass


    class HasVertex(Base):
        __tablename__ = "has_vertex"
        id: Mapped[int] = mapped_column(primary_key=True)
        x1: Mapped[int]
        y1: Mapped[int]
        x2: Mapped[int]
        y2: Mapped[int]

        vertex: Mapped[Vertex] = composite(Vertex._generate, "x1", "y1", "x2", "y2")

The above mapping can then be used in terms of ``HasVertex``, ``Vertex``, and
``Point``::

    hv = HasVertex(vertex=Vertex(Point(1, 2), Point(3, 4)))

    session.add(hv)
    session.commit()

    stmt = select(HasVertex).where(HasVertex.vertex == Vertex(Point(1, 2), Point(3, 4)))

    hv = session.scalars(stmt).first()
    print(hv.vertex.start)
    print(hv.vertex.end)

.. _dataclass: https://docs.python.org/3/library/dataclasses.html

Composite API
-------------

.. autofunction:: composite