File: declarative_tables.rst

package info (click to toggle)
sqlalchemy 1.4.46%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,444 kB
  • sloc: python: 341,434; ansic: 1,760; makefile: 226; xml: 17; sh: 7
file content (456 lines) | stat: -rw-r--r-- 16,422 bytes parent folder | download
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

.. _orm_declarative_table_config_toplevel:

=============================================
Table Configuration with Declarative
=============================================

As introduced at :ref:`orm_declarative_mapping`, the Declarative style
includes the ability to generate a mapped :class:`_schema.Table` object
at the same time, or to accommodate a :class:`_schema.Table` or other
:class:`_sql.FromClause` object directly.

The following examples assume a declarative base class as::

    from sqlalchemy.orm import declarative_base

    Base = declarative_base()

All of the examples that follow illustrate a class inheriting from the above
``Base``.  The decorator style introduced at :ref:`orm_declarative_decorator`
is fully supported with all the following examples as well.

.. _orm_declarative_table:

Declarative Table
-----------------

With the declarative base class, the typical form of mapping includes an
attribute ``__tablename__`` that indicates the name of a :class:`_schema.Table`
that should be generated along with the mapping::

    from sqlalchemy import Column, ForeignKey, Integer, String
    from sqlalchemy.orm import declarative_base

    Base = declarative_base()


    class User(Base):
        __tablename__ = "user"

        id = Column(Integer, primary_key=True)
        name = Column(String)
        fullname = Column(String)
        nickname = Column(String)

Above, :class:`_schema.Column` objects are placed inline with the class
definition.   The declarative mapping process will generate a new
:class:`_schema.Table` object against the :class:`_schema.MetaData` collection
associated with the declarative base, and each specified
:class:`_schema.Column` object will become part of the :attr:`.schema.Table.columns`
collection of this :class:`_schema.Table` object.   The :class:`_schema.Column`
objects can omit their "name" field, which is usually the first positional
argument to the :class:`_schema.Column` constructor; the declarative system
will assign the key associated with each :class:`_schema.Column` as the name,
to produce a :class:`_schema.Table` that is equivalent to::

    # equivalent Table object produced
    user_table = Table(
        "user",
        Base.metadata,
        Column("id", Integer, primary_key=True),
        Column("name", String),
        Column("fullname", String),
        Column("nickname", String),
    )

.. seealso::

    :ref:`mapping_columns_toplevel` - contains additional notes on affecting
    how :class:`_orm.Mapper` interprets incoming :class:`.Column` objects.

.. _orm_declarative_metadata:

Accessing Table and Metadata
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A declaratively mapped class will always include an attribute called
``__table__``; when the above configuration using ``__tablename__`` is
complete, the declarative process makes the :class:`_schema.Table`
available via the ``__table__`` attribute::


    # access the Table
    user_table = User.__table__

The above table is ultimately the same one that corresponds to the
:attr:`_orm.Mapper.local_table` attribute, which we can see through the
:ref:`runtime inspection system <inspection_toplevel>`::

    from sqlalchemy import inspect

    user_table = inspect(User).local_table

The :class:`_schema.MetaData` collection associated with both the declarative
:class:`_orm.registry` as well as the base class is frequently necessary in
order to run DDL operations such as CREATE, as well as in use with migration
tools such as Alembic.   This object is available via the ``.metadata``
attribute of :class:`_orm.registry` as well as the declarative base class.
Below, for a small script we may wish to emit a CREATE for all tables against a
SQLite database::

    engine = create_engine("sqlite://")

    Base.metadata.create_all(engine)

.. _orm_declarative_table_configuration:

Declarative Table Configuration
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

When using Declarative Table configuration with the ``__tablename__``
declarative class attribute, additional arguments to be supplied to the
:class:`_schema.Table` constructor should be provided using the
``__table_args__`` declarative class attribute.

This attribute accommodates both positional as well as keyword
arguments that are normally sent to the
:class:`_schema.Table` constructor.
The attribute can be specified in one of two forms. One is as a
dictionary::

    class MyClass(Base):
        __tablename__ = "sometable"
        __table_args__ = {"mysql_engine": "InnoDB"}

The other, a tuple, where each argument is positional
(usually constraints)::

    class MyClass(Base):
        __tablename__ = "sometable"
        __table_args__ = (
            ForeignKeyConstraint(["id"], ["remote_table.id"]),
            UniqueConstraint("foo"),
        )

Keyword arguments can be specified with the above form by
specifying the last argument as a dictionary::

    class MyClass(Base):
        __tablename__ = "sometable"
        __table_args__ = (
            ForeignKeyConstraint(["id"], ["remote_table.id"]),
            UniqueConstraint("foo"),
            {"autoload": True},
        )

A class may also specify the ``__table_args__`` declarative attribute,
as well as the ``__tablename__`` attribute, in a dynamic style using the
:func:`_orm.declared_attr` method decorator.   See the section
:ref:`declarative_mixins` for examples on how this is often used.

.. _orm_declarative_table_schema_name:

Explicit Schema Name with Declarative Table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The schema name for a :class:`_schema.Table` as documented at
:ref:`schema_table_schema_name` is applied to an individual :class:`_schema.Table`
using the :paramref:`_schema.Table.schema` argument.   When using Declarative
tables, this option is passed like any other to the ``__table_args__``
dictionary::


    class MyClass(Base):
        __tablename__ = "sometable"
        __table_args__ = {"schema": "some_schema"}

The schema name can also be applied to all :class:`_schema.Table` objects
globally by using the :paramref:`_schema.MetaData.schema` parameter documented
at :ref:`schema_metadata_schema_name`.   The :class:`_schema.MetaData` object
may be constructed separately and passed either to :func:`_orm.registry`
or :func:`_orm.declarative_base`::

    from sqlalchemy import MetaData

    metadata_obj = MetaData(schema="some_schema")

    Base = declarative_base(metadata=metadata_obj)


    class MyClass(Base):
        # will use "some_schema" by default
        __tablename__ = "sometable"

.. seealso::

    :ref:`schema_table_schema_name` - in the :ref:`metadata_toplevel` documentation.

.. _orm_declarative_table_adding_columns:

Appending additional columns to an existing Declarative mapped class
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A declarative table configuration allows the addition of new
:class:`_schema.Column` objects to an existing mapping after the :class:`.Table`
metadata has already been generated.

For a declarative class that is declared using a declarative base class,
the underlying metaclass :class:`.DeclarativeMeta` includes a ``__setattr__()``
method that will intercept additional :class:`.Column` objects and
add them to both the :class:`.Table` using :meth:`.Table.append_column`
as well as to the existing :class:`.Mapper` using :meth:`.Mapper.add_property`::

    MyClass.some_new_column = Column("data", Unicode)

Additional :class:`_schema.Column` objects may also be added to a mapping
in the specific circumstance of using single table inheritance, where
additional columns are present on mapped subclasses that have
no :class:`.Table` of their own.  This is illustrated in the section
:ref:`single_inheritance`.

.. _orm_imperative_table_configuration:

Declarative with Imperative Table (a.k.a. Hybrid Declarative)
-------------------------------------------------------------

Declarative mappings may also be provided with a pre-existing
:class:`_schema.Table` object, or otherwise a :class:`_schema.Table` or other
arbitrary :class:`_sql.FromClause` construct (such as a :class:`_sql.Join`
or :class:`_sql.Subquery`) that is constructed separately.

This is referred to as a "hybrid declarative"
mapping, as the class is mapped using the declarative style for everything
involving the mapper configuration, however the mapped :class:`_schema.Table`
object is produced separately and passed to the declarative process
directly::


    from sqlalchemy import Column, ForeignKey, Integer, String
    from sqlalchemy.orm import declarative_base

    Base = declarative_base()

    # construct a Table directly.  The Base.metadata collection is
    # usually a good choice for MetaData but any MetaData
    # collection may be used.

    user_table = Table(
        "user",
        Base.metadata,
        Column("id", Integer, primary_key=True),
        Column("name", String),
        Column("fullname", String),
        Column("nickname", String),
    )


    # construct the User class using this table.
    class User(Base):
        __table__ = user_table

Above, a :class:`_schema.Table` object is constructed using the approach
described at :ref:`metadata_describing`.   It can then be applied directly
to a class that is declaratively mapped.  The ``__tablename__`` and
``__table_args__`` declarative class attributes are not used in this form.
The above configuration is often more readable as an inline definition::

    class User(Base):
        __table__ = Table(
            "user",
            Base.metadata,
            Column("id", Integer, primary_key=True),
            Column("name", String),
            Column("fullname", String),
            Column("nickname", String),
        )

A natural effect of the above style is that the ``__table__`` attribute is
itself defined within the class definition block.   As such it may be
immediately referred towards within subsequent attributes, such as the example
below which illustrates referring to the ``type`` column in a polymorphic
mapper configuration::

    class Person(Base):
        __table__ = Table(
            "person",
            Base.metadata,
            Column("id", Integer, primary_key=True),
            Column("name", String(50)),
            Column("type", String(50)),
        )

        __mapper_args__ = {
            "polymorphic_on": __table__.c.type,
            "polymorhpic_identity": "person",
        }

The "imperative table" form is also used when a non-:class:`_schema.Table`
construct, such as a :class:`_sql.Join` or :class:`_sql.Subquery` object,
is to be mapped.  An example below::

    from sqlalchemy import func, select

    subq = (
        select(
            func.count(orders.c.id).label("order_count"),
            func.max(orders.c.price).label("highest_order"),
            orders.c.customer_id,
        )
        .group_by(orders.c.customer_id)
        .subquery()
    )

    customer_select = (
        select(customers, subq)
        .join_from(customers, subq, customers.c.id == subq.c.customer_id)
        .subquery()
    )


    class Customer(Base):
        __table__ = customer_select

For background on mapping to non-:class:`_schema.Table` constructs see
the sections :ref:`orm_mapping_joins` and :ref:`orm_mapping_arbitrary_subqueries`.

The "imperative table" form is of particular use when the class itself
is using an alternative form of attribute declaration, such as Python
dataclasses.   See the section :ref:`orm_declarative_dataclasses` for detail.

.. seealso::

    :ref:`metadata_describing`

    :ref:`orm_declarative_dataclasses`

.. _orm_declarative_reflected:

Mapping Declaratively with Reflected Tables
--------------------------------------------

There are several patterns available which provide for producing mapped
classes against a series of :class:`_schema.Table` objects that were
introspected from the database, using the reflection process described at
:ref:`metadata_reflection`.

A very simple way to map a class to a table reflected from the database is to
use a declarative hybrid mapping, passing the
:paramref:`_schema.Table.autoload_with` parameter to the
:class:`_schema.Table`::

    from sqlalchemy import create_engine
    from sqlalchemy import Table
    from sqlalchemy.orm import declarative_base

    engine = create_engine("postgresql+psycopg2://user:pass@hostname/my_existing_database")

    Base = declarative_base()


    class MyClass(Base):
        __table__ = Table(
            "mytable",
            Base.metadata,
            autoload_with=engine,
        )

A variant on the above pattern that scales much better is to use the
:meth:`.MetaData.reflect` method to reflect a full set of :class:`.Table`
objects at once, then refer to them from the :class:`.MetaData`::


    from sqlalchemy import create_engine
    from sqlalchemy import Table
    from sqlalchemy.orm import declarative_base

    engine = create_engine("postgresql+psycopg2://user:pass@hostname/my_existing_database")

    Base = declarative_base()

    Base.metadata.reflect(engine)


    class MyClass(Base):
        __table__ = Base.metadata.tables["mytable"]

.. seealso::

    :ref:`mapper_automated_reflection_schemes` - further notes on using
    table reflection with mapped classes

A major downside to the above approach is that the mapped classes cannot
be declared until the tables have been reflected, which requires the database
connectivity source to be present while the application classes are being
declared; it's typical that classes are declared as the modules of an
application are being imported, but database connectivity isn't available
until the application starts running code so that it can consume configuration
information and create an engine.   There are currently two approaches
to working around this.

.. _orm_declarative_reflected_deferred_reflection:

Using DeferredReflection
^^^^^^^^^^^^^^^^^^^^^^^^^

To accommodate the use case of declaring mapped classes where reflection of
table metadata can occur afterwards, a simple extension called the
:class:`.DeferredReflection` mixin is available, which alters the declarative
mapping process to be delayed until a special class-level
:meth:`.DeferredReflection.prepare` method is called, which will perform
the reflection process against a target database, and will integrate the
results with the declarative table mapping process, that is, classes which
use the ``__tablename__`` attribute::

    from sqlalchemy.ext.declarative import DeferredReflection
    from sqlalchemy.orm import declarative_base

    Base = declarative_base()


    class Reflected(DeferredReflection):
        __abstract__ = True


    class Foo(Reflected, Base):
        __tablename__ = "foo"
        bars = relationship("Bar")


    class Bar(Reflected, Base):
        __tablename__ = "bar"

        foo_id = Column(Integer, ForeignKey("foo.id"))

Above, we create a mixin class ``Reflected`` that will serve as a base
for classes in our declarative hierarchy that should become mapped when
the ``Reflected.prepare`` method is called.   The above mapping is not
complete until we do so, given an :class:`_engine.Engine`::


    engine = create_engine("postgresql+psycopg2://user:pass@hostname/my_existing_database")
    Reflected.prepare(engine)

The purpose of the ``Reflected`` class is to define the scope at which
classes should be reflectively mapped.   The plugin will search among the
subclass tree of the target against which ``.prepare()`` is called and reflect
all tables which are named by declared classes; tables in the target database
that are not part of mappings and are not related to the target tables
via foreign key constraint will not be reflected.

Using Automap
^^^^^^^^^^^^^^

A more automated solution to mapping against an existing database where table
reflection is to be used is to use the :ref:`automap_toplevel` extension. This
extension will generate entire mapped classes from a database schema, including
relationships between classes based on observed foreign key constraints. While
it includes hooks for customization, such as hooks that allow custom
class naming and relationship naming schemes, automap is oriented towards an
expedient zero-configuration style of working. If an application wishes to have
a fully explicit model that makes use of table reflection, the
:ref:`orm_declarative_reflected_deferred_reflection` may be preferable.

.. seealso::

    :ref:`automap_toplevel`