Mapper Configuration with Declarative

The section Mapped Class Essential Components discusses the general configurational elements of a Mapper construct, which is the structure that defines how a particular user defined class is mapped to a database table or other SQL construct. The following sections describe specific details about how the declarative system goes about constructing the Mapper.

Defining Mapped Properties with Declarative

The examples given at Table Configuration with Declarative illustrate mappings against table-bound columns, using the mapped_column() construct. There are several other varieties of ORM mapped constructs that may be configured besides table-bound columns, the most common being the relationship() construct. Other kinds of properties include SQL expressions that are defined using the column_property() construct and multiple-column mappings using the composite() construct.

While an imperative mapping makes use of the properties dictionary to establish all the mapped class attributes, in the declarative mapping, these properties are all specified inline with the class definition, which in the case of a declarative table mapping are inline with the Column objects that will be used to generate a Table object.

Working with the example mapping of User and Address, we may illustrate a declarative table mapping that includes not just mapped_column() objects but also relationships and SQL expressions:

  1. from typing import List
  2. from typing import Optional
  3. from sqlalchemy import Column
  4. from sqlalchemy import ForeignKey
  5. from sqlalchemy import String
  6. from sqlalchemy import Text
  7. from sqlalchemy.orm import column_property
  8. from sqlalchemy.orm import DeclarativeBase
  9. from sqlalchemy.orm import Mapped
  10. from sqlalchemy.orm import mapped_column
  11. from sqlalchemy.orm import relationship
  12. class Base(DeclarativeBase):
  13. pass
  14. class User(Base):
  15. __tablename__ = "user"
  16. id: Mapped[int] = mapped_column(primary_key=True)
  17. name: Mapped[str]
  18. firstname: Mapped[str] = mapped_column(String(50))
  19. lastname: Mapped[str] = mapped_column(String(50))
  20. fullname: Mapped[str] = column_property(firstname + " " + lastname)
  21. addresses: Mapped[List["Address"]] = relationship(back_populates="user")
  22. class Address(Base):
  23. __tablename__ = "address"
  24. id: Mapped[int] = mapped_column(primary_key=True)
  25. user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
  26. email_address: Mapped[str]
  27. address_statistics: Mapped[Optional[str]] = mapped_column(Text, deferred=True)
  28. user: Mapped["User"] = relationship(back_populates="addresses")

The above declarative table mapping features two tables, each with a relationship() referring to the other, as well as a simple SQL expression mapped by column_property(), and an additional mapped_column() that indicates loading should be on a “deferred” basis as defined by the mapped_column.deferred keyword. More documentation on these particular concepts may be found at Basic Relationship Patterns, Using column_property, and Limiting which Columns Load with Column Deferral.

Properties may be specified with a declarative mapping as above using “hybrid table” style as well; the Column objects that are directly part of a table move into the Table definition but everything else, including composed SQL expressions, would still be inline with the class definition. Constructs that need to refer to a Column directly would reference it in terms of the Table object. To illustrate the above mapping using hybrid table style:

  1. # mapping attributes using declarative with imperative table
  2. # i.e. __table__
  3. from sqlalchemy import Column, ForeignKey, Integer, String, Table, Text
  4. from sqlalchemy.orm import column_property
  5. from sqlalchemy.orm import DeclarativeBase
  6. from sqlalchemy.orm import deferred
  7. from sqlalchemy.orm import relationship
  8. class Base(DeclarativeBase):
  9. pass
  10. class User(Base):
  11. __table__ = Table(
  12. "user",
  13. Base.metadata,
  14. Column("id", Integer, primary_key=True),
  15. Column("name", String),
  16. Column("firstname", String(50)),
  17. Column("lastname", String(50)),
  18. )
  19. fullname = column_property(__table__.c.firstname + " " + __table__.c.lastname)
  20. addresses = relationship("Address", back_populates="user")
  21. class Address(Base):
  22. __table__ = Table(
  23. "address",
  24. Base.metadata,
  25. Column("id", Integer, primary_key=True),
  26. Column("user_id", ForeignKey("user.id")),
  27. Column("email_address", String),
  28. Column("address_statistics", Text),
  29. )
  30. address_statistics = deferred(__table__.c.address_statistics)
  31. user = relationship("User", back_populates="addresses")

Things to note above:

  • The address Table contains a column called address_statistics, however we re-map this column under the same attribute name to be under the control of a deferred() construct.

  • With both declararative table and hybrid table mappings, when we define a ForeignKey construct, we always name the target table using the table name, and not the mapped class name.

  • When we define relationship() constructs, as these constructs create a linkage between two mapped classes where one necessarily is defined before the other, we can refer to the remote class using its string name. This functionality also extends into the area of other arguments specified on the relationship() such as the “primary join” and “order by” arguments. See the section Late-Evaluation of Relationship Arguments for details on this.

Mapper Configuration Options with Declarative

With all mapping forms, the mapping of the class is configured through parameters that become part of the Mapper object. The function which ultimately receives these arguments is the Mapper function, and are delivered to it from one of the front-facing mapping functions defined on the registry object.

For the declarative form of mapping, mapper arguments are specified using the __mapper_args__ declarative class variable, which is a dictionary that is passed as keyword arguments to the Mapper function. Some examples:

Map Specific Primary Key Columns

The example below illustrates Declarative-level settings for the Mapper.primary_key parameter, which establishes particular columns as part of what the ORM should consider to be a primary key for the class, independently of schema-level primary key constraints:

  1. class GroupUsers(Base):
  2. __tablename__ = "group_users"
  3. user_id = mapped_column(String(40))
  4. group_id = mapped_column(String(40))
  5. __mapper_args__ = {"primary_key": [user_id, group_id]}

See also

Mapping to an Explicit Set of Primary Key Columns - further background on ORM mapping of explicit columns as primary key columns

Version ID Column

The example below illustrates Declarative-level settings for the Mapper.version_id_col and Mapper.version_id_generator parameters, which configure an ORM-maintained version counter that is updated and checked within the unit of work flush process:

  1. from datetime import datetime
  2. class Widget(Base):
  3. __tablename__ = "widgets"
  4. id = mapped_column(Integer, primary_key=True)
  5. timestamp = mapped_column(DateTime, nullable=False)
  6. __mapper_args__ = {
  7. "version_id_col": timestamp,
  8. "version_id_generator": lambda v: datetime.now(),
  9. }

See also

Configuring a Version Counter - background on the ORM version counter feature

Single Table Inheritance

The example below illustrates Declarative-level settings for the Mapper.polymorphic_on and Mapper.polymorphic_identity parameters, which are used when configuring a single-table inheritance mapping:

  1. class Person(Base):
  2. __tablename__ = "person"
  3. person_id = mapped_column(Integer, primary_key=True)
  4. type = mapped_column(String, nullable=False)
  5. __mapper_args__ = dict(
  6. polymorphic_on=type,
  7. polymorphic_identity="person",
  8. )
  9. class Employee(Person):
  10. __mapper_args__ = dict(
  11. polymorphic_identity="employee",
  12. )

See also

Single Table Inheritance - background on the ORM single table inheritance mapping feature.

Constructing mapper arguments dynamically

The __mapper_args__ dictionary may be generated from a class-bound descriptor method rather than from a fixed dictionary by making use of the declared_attr() construct. This is useful to create arguments for mappers that are programmatically derived from the table configuration or other aspects of the mapped class. A dynamic __mapper_args__ attribute will typically be useful when using a Declarative Mixin or abstract base class.

For example, to omit from the mapping any columns that have a special Column.info value, a mixin can use a __mapper_args__ method that scans for these columns from the cls.__table__ attribute and passes them to the Mapper.exclude_properties collection:

  1. from sqlalchemy import Column
  2. from sqlalchemy import Integer
  3. from sqlalchemy import select
  4. from sqlalchemy import String
  5. from sqlalchemy.orm import DeclarativeBase
  6. from sqlalchemy.orm import declared_attr
  7. class ExcludeColsWFlag:
  8. @declared_attr
  9. def __mapper_args__(cls):
  10. return {
  11. "exclude_properties": [
  12. column.key
  13. for column in cls.__table__.c
  14. if column.info.get("exclude", False)
  15. ]
  16. }
  17. class Base(DeclarativeBase):
  18. pass
  19. class SomeClass(ExcludeColsWFlag, Base):
  20. __tablename__ = "some_table"
  21. id = mapped_column(Integer, primary_key=True)
  22. data = mapped_column(String)
  23. not_needed = mapped_column(String, info={"exclude": True})

Above, the ExcludeColsWFlag mixin provides a per-class __mapper_args__ hook that will scan for Column objects that include the key/value 'exclude': True passed to the Column.info parameter, and then add their string “key” name to the Mapper.exclude_properties collection which will prevent the resulting Mapper from considering these columns for any SQL operations.

See also

Composing Mapped Hierarchies with Mixins

Other Declarative Mapping Directives

__declare_last__()

The __declare_last__() hook allows definition of a class level function that is automatically called by the MapperEvents.after_configured() event, which occurs after mappings are assumed to be completed and the ‘configure’ step has finished:

  1. class MyClass(Base):
  2. @classmethod
  3. def __declare_last__(cls):
  4. """ """
  5. # do something with mappings

__declare_first__()

Like __declare_last__(), but is called at the beginning of mapper configuration via the MapperEvents.before_configured() event:

  1. class MyClass(Base):
  2. @classmethod
  3. def __declare_first__(cls):
  4. """ """
  5. # do something before mappings are configured

New in version 0.9.3.

metadata

The MetaData collection normally used to assign a new Table is the registry.metadata attribute associated with the registry object in use. When using a declarative base class such as that produced by the DeclarativeBase superclass, as well as legacy functions such as declarative_base() and registry.generate_base(), this MetaData is also normally present as an attribute named .metadata that’s directly on the base class, and thus also on the mapped class via inheritance. Declarative uses this attribute, when present, in order to determine the target MetaData collection, or if not present, uses the MetaData associated directly with the registry.

This attribute may also be assigned towards in order to affect the MetaData collection to be used on a per-mapped-hierarchy basis for a single base and/or registry. This takes effect whether a declarative base class is used or if the registry.mapped() decorator is used directly, thus allowing patterns such as the metadata-per-abstract base example in the next section, __abstract__. A similar pattern can be illustrated using registry.mapped() as follows:

  1. reg = registry()
  2. class BaseOne:
  3. metadata = MetaData()
  4. class BaseTwo:
  5. metadata = MetaData()
  6. @reg.mapped
  7. class ClassOne:
  8. __tablename__ = "t1" # will use reg.metadata
  9. id = mapped_column(Integer, primary_key=True)
  10. @reg.mapped
  11. class ClassTwo(BaseOne):
  12. __tablename__ = "t1" # will use BaseOne.metadata
  13. id = mapped_column(Integer, primary_key=True)
  14. @reg.mapped
  15. class ClassThree(BaseTwo):
  16. __tablename__ = "t1" # will use BaseTwo.metadata
  17. id = mapped_column(Integer, primary_key=True)

See also

__abstract__

__abstract__

__abstract__ causes declarative to skip the production of a table or mapper for the class entirely. A class can be added within a hierarchy in the same way as mixin (see Mixin and Custom Base Classes), allowing subclasses to extend just from the special class:

  1. class SomeAbstractBase(Base):
  2. __abstract__ = True
  3. def some_helpful_method(self):
  4. """ """
  5. @declared_attr
  6. def __mapper_args__(cls):
  7. return {"helpful mapper arguments": True}
  8. class MyMappedClass(SomeAbstractBase):
  9. pass

One possible use of __abstract__ is to use a distinct MetaData for different bases:

  1. class Base(DeclarativeBase):
  2. pass
  3. class DefaultBase(Base):
  4. __abstract__ = True
  5. metadata = MetaData()
  6. class OtherBase(Base):
  7. __abstract__ = True
  8. metadata = MetaData()

Above, classes which inherit from DefaultBase will use one MetaData as the registry of tables, and those which inherit from OtherBase will use a different one. The tables themselves can then be created perhaps within distinct databases:

  1. DefaultBase.metadata.create_all(some_engine)
  2. OtherBase.metadata.create_all(some_other_engine)

See also

Building Deeper Hierarchies with polymorphic_abstract - an alternative form of “abstract” mapped class that is appropriate for inheritance hierarchies.

__table_cls__

Allows the callable / class used to generate a Table to be customized. This is a very open-ended hook that can allow special customizations to a Table that one generates here:

  1. class MyMixin:
  2. @classmethod
  3. def __table_cls__(cls, name, metadata_obj, *arg, **kw):
  4. return Table(f"my_{name}", metadata_obj, *arg, **kw)

The above mixin would cause all Table objects generated to include the prefix "my_", followed by the name normally specified using the __tablename__ attribute.

__table_cls__ also supports the case of returning None, which causes the class to be considered as single-table inheritance vs. its subclass. This may be useful in some customization schemes to determine that single-table inheritance should take place based on the arguments for the table itself, such as, define as single-inheritance if there is no primary key present:

  1. class AutoTable:
  2. @declared_attr
  3. def __tablename__(cls):
  4. return cls.__name__
  5. @classmethod
  6. def __table_cls__(cls, *arg, **kw):
  7. for obj in arg[1:]:
  8. if (isinstance(obj, Column) and obj.primary_key) or isinstance(
  9. obj, PrimaryKeyConstraint
  10. ):
  11. return Table(*arg, **kw)
  12. return None
  13. class Person(AutoTable, Base):
  14. id = mapped_column(Integer, primary_key=True)
  15. class Employee(Person):
  16. employee_name = mapped_column(String)

The above Employee class would be mapped as single-table inheritance against Person; the employee_name column would be added as a member of the Person table.