Composing Mapped Hierarchies with Mixins

    Tip

    In addition to mixin classes, common column options may also be shared among many classes using PEP 593 types; see and Mapping Whole Column Declarations to Python Types for background on these SQLAlchemy 2.0 features.

    An example of some commonly mixed-in idioms is below:

    The above example illustrates a class MyModel which includes two mixins CommonMixin and HasLogRecord in its bases, as well as a supplementary class LogRecord which also includes CommonMixin, demonstrating a variety of constructs that are supported on mixins and base classes, including:

    • columns declared using , Mapped or are copied from mixins or base classes onto the target class to be mapped; above this is illustrated via the column attributes CommonMixin.id and HasLogRecord.log_record_id.

    • Declarative directives such as __table_args__ and __mapper_args__ can be assigned to a mixin or base class, where they will take effect automatically for any classes which inherit from the mixin or base. The above example illustrates this using the __table_args__ and __mapper_args__ attributes.

    • All Declarative directives, including all of __tablename__, __table__, __table_args__ and __mapper_args__, may be implemented using user-defined class methods, which are decorated with the declared_attr decorator (specifically the sub-member, more on that in a moment). Above, this is illustrated using a def __tablename__(cls) classmethod that generates a Table name dynamically; when applied to the MyModel class, the table name will be generated as "mymodel", and when applied to the LogRecord class, the table name will be generated as "logrecord".

    • Other ORM properties such as can be generated on the target class to be mapped using user-defined class methods also decorated with the declared_attr decorator. Above, this is illustrated by generating a many-to-one to a mapped object called LogRecord.

    The features above may all be demonstrated using a select() example:

    1. >>> from sqlalchemy import select
    2. >>> print(select(MyModel).join(MyModel.log_record))
    3. SELECT mymodel.name, mymodel.id, mymodel.log_record_id
    4. FROM mymodel JOIN logrecord ON logrecord.id = mymodel.log_record_id

    Tip

    The examples of will attempt to illustrate the correct PEP 484 annotations for each method example. The use of annotations with functions are completely optional, and are not consumed by Declarative; however, these annotations are required in order to pass Mypy --strict type checking.

    Additionally, the declared_attr.directive sub-member illustrated above is optional as well, and is only significant for typing tools, as it adjusts for the expected return type when creating methods to override Declarative directives such as __tablename__, __mapper_args__ and __table_args__.

    New in version 2.0: As part of PEP 484 typing support for the SQLAlchemy ORM, added the to declared_attr to distinguish between attributes and Declarative configurational attributes

    There’s no fixed convention for the order of mixins and base classes. Normal Python method resolution rules apply, and the above example would work just as well with:

    1. class MyModel(Base, HasLogRecord, CommonMixin):
    2. name: Mapped[str] = mapped_column()

    This works because Base here doesn’t define any of the variables that CommonMixin or HasLogRecord defines, i.e. __tablename__, __table_args__, id, etc. If the Base did define an attribute of the same name, the class placed first in the inherits list would determine which attribute is used on the newly defined class.

    Tip

    While the above example is using Annotated Declarative Table form based on the annotation class, mixin classes also work perfectly well with non-annotated and legacy Declarative forms, such as when using Column directly instead of .

    In addition to using a pure mixin, most of the techniques in this section can also be applied to the base class directly, for patterns that should apply to all classes derived from a particular base. The example below illustrates some of the the previous section’s example in terms of the Base class:

    1. from sqlalchemy import ForeignKey
    2. from sqlalchemy.orm import declared_attr
    3. from sqlalchemy.orm import DeclarativeBase
    4. from sqlalchemy.orm import Mapped
    5. from sqlalchemy.orm import mapped_column
    6. from sqlalchemy.orm import relationship
    7. class Base(DeclarativeBase):
    8. """define a series of common elements that may be applied to mapped
    9. classes using this class as a base class."""
    10. @declared_attr.directive
    11. def __tablename__(cls) -> str:
    12. return cls.__name__.lower()
    13. __table_args__ = {"mysql_engine": "InnoDB"}
    14. __mapper_args__ = {"eager_defaults": True}
    15. id: Mapped[int] = mapped_column(primary_key=True)
    16. class HasLogRecord:
    17. """mark classes that have a many-to-one relationship to the
    18. ``LogRecord`` class."""
    19. log_record_id: Mapped[int] = mapped_column(ForeignKey("logrecord.id"))
    20. @declared_attr
    21. def log_record(self) -> Mapped["LogRecord"]:
    22. return relationship("LogRecord")
    23. class LogRecord(Base):
    24. log_info: Mapped[str]
    25. class MyModel(HasLogRecord, Base):
    26. name: Mapped[str]

    Where above, MyModel as well as LogRecord, in deriving from Base, will both have their table name derived from their class name, a primary key column named id, as well as the above table and mapper arguments defined by Base.__table_args__ and Base.__mapper_args__.

    When using legacy or registry.generate_base(), the parameter may be used as follows to generate an equivalent effect, as illustrated in the non-annotated example below:

    1. # legacy declarative_base() use
    2. from sqlalchemy import Integer, String
    3. from sqlalchemy import ForeignKey
    4. from sqlalchemy.orm import declared_attr
    5. from sqlalchemy.orm import declarative_base
    6. from sqlalchemy.orm import mapped_column
    7. from sqlalchemy.orm import relationship
    8. class Base:
    9. """define a series of common elements that may be applied to mapped
    10. classes using this class as a base class."""
    11. @declared_attr.directive
    12. def __tablename__(cls):
    13. return cls.__name__.lower()
    14. __table_args__ = {"mysql_engine": "InnoDB"}
    15. __mapper_args__ = {"eager_defaults": True}
    16. id = mapped_column(Integer, primary_key=True)
    17. Base = declarative_base(cls=Base)
    18. class HasLogRecord:
    19. """mark classes that have a many-to-one relationship to the
    20. ``LogRecord`` class."""
    21. log_record_id = mapped_column(ForeignKey("logrecord.id"))
    22. @declared_attr
    23. def log_record(self):
    24. return relationship("LogRecord")
    25. class LogRecord(Base):
    26. log_info = mapped_column(String)
    27. class MyModel(HasLogRecord, Base):
    28. name = mapped_column(String)

    Mixing in Columns

    Columns can be indicated in mixins assuming the style of configuration is in use (as opposed to imperative table configuration), so that columns declared on the mixin can then be copied to be part of the that the Declarative process generates. All three of the mapped_column(), , and Column constructs may be declared inline in a declarative mixin:

    1. class TimestampMixin:
    2. created_at: Mapped[datetime] = mapped_column(default=func.now())
    3. updated_at: Mapped[datetime]
    4. class MyModel(TimestampMixin, Base):
    5. __tablename__ = "test"
    6. id: Mapped[int] = mapped_column(primary_key=True)
    7. name: Mapped[str]

    Where above, all declarative classes that include TimestampMixin in their class bases will automatically include a column created_at that applies a timestamp to all row insertions, as well as an updated_at column, which does not include a default for the purposes of the example (if it did, we would use the parameter which is accepted by mapped_column()). These column constructs are always copied from the originating mixin or base class, so that the same mixin/base class may be applied to any number of target classes which will each have their own column constructs.

    All Declarative column forms are supported by mixins, including:

    • Annotated attributes - with or without present:

      1. class TimestampMixin:
      2. created_at: Mapped[datetime] = mapped_column(default=func.now())
      3. updated_at: Mapped[datetime]
    • mapped_column - with or without Mapped present:

    • Column - legacy Declarative form:

      1. updated_at = Column(DateTime)

    In each of the above forms, Declarative handles the column-based attributes on the mixin class by creating a copy of the construct, which is then applied to the target class.

    Changed in version 2.0: The declarative API can now accommodate objects as well as mapped_column() constructs of any form when using mixins without the need to use . Previous limitations which prevented columns with ForeignKey elements from being used directly in mixins have been removed.

    Relationships created by relationship() are provided with declarative mixin classes exclusively using the approach, eliminating any ambiguity which could arise when copying a relationship and its possibly column-bound contents. Below is an example which combines a foreign key column and a relationship so that two classes Foo and Bar can both be configured to reference a common target class via many-to-one:

    1. from sqlalchemy import ForeignKey
    2. from sqlalchemy.orm import DeclarativeBase
    3. from sqlalchemy.orm import declared_attr
    4. from sqlalchemy.orm import Mapped
    5. from sqlalchemy.orm import mapped_column
    6. from sqlalchemy.orm import relationship
    7. class Base(DeclarativeBase):
    8. pass
    9. class RefTargetMixin:
    10. target_id: Mapped[int] = mapped_column(ForeignKey("target.id"))
    11. @declared_attr
    12. def target(cls) -> Mapped["Target"]:
    13. return relationship("Target")
    14. class Foo(RefTargetMixin, Base):
    15. __tablename__ = "foo"
    16. id: Mapped[int] = mapped_column(primary_key=True)
    17. class Bar(RefTargetMixin, Base):
    18. __tablename__ = "bar"
    19. id: Mapped[int] = mapped_column(primary_key=True)
    20. class Target(Base):
    21. __tablename__ = "target"
    22. id: Mapped[int] = mapped_column(primary_key=True)

    With the above mapping, each of Foo and Bar contain a relationship to Target accessed along the .target attribute:

    1. >>> from sqlalchemy import select
    2. >>> print(select(Foo).join(Foo.target))
    3. SELECT foo.id, foo.target_id
    4. FROM foo JOIN target ON target.id = foo.target_id
    5. >>> print(select(Bar).join(Bar.target))
    6. SELECT bar.id, bar.target_id
    7. FROM bar JOIN target ON target.id = bar.target_id

    Special arguments such as relationship.primaryjoin may also be used within mixed-in classmethods, which often need to refer to the class that’s being mapped. For schemes that need to refer to locally mapped columns, in ordinary cases these columns are made available by Declarative as attributes on the mapped class which is passed as the cls argument to the decorated classmethod. Using this feature, we could for example rewrite the RefTargetMixin.target method using an explicit primaryjoin which refers to pending mapped columns on both Target and cls:

    1. class Target(Base):
    2. __tablename__ = "target"
    3. id: Mapped[int] = mapped_column(primary_key=True)
    4. class RefTargetMixin:
    5. target_id: Mapped[int] = mapped_column(ForeignKey("target.id"))
    6. @declared_attr
    7. def target(cls) -> Mapped["Target"]:
    8. # illustrates explicit 'primaryjoin' argument
    9. return relationship("Target", primaryjoin=Target.id == cls.target_id)

    Mixing in column_property() and other classes

    Like relationship(), other subclasses such as column_property() also need to have class-local copies generated when used by mixins, so are also declared within functions that are decorated by . Within the function, other ordinary mapped columns that were declared with mapped_column(), , or Column will be made available from the cls argument so that they may be used to compose new attributes, as in the example below which adds two columns together:

    1. from sqlalchemy.orm import column_property
    2. from sqlalchemy.orm import DeclarativeBase
    3. from sqlalchemy.orm import declared_attr
    4. from sqlalchemy.orm import Mapped
    5. from sqlalchemy.orm import mapped_column
    6. class Base(DeclarativeBase):
    7. pass
    8. class SomethingMixin:
    9. x: Mapped[int]
    10. y: Mapped[int]
    11. @declared_attr
    12. def x_plus_y(cls) -> Mapped[int]:
    13. return column_property(cls.x + cls.y)
    14. class Something(SomethingMixin, Base):
    15. __tablename__ = "something"
    16. id: Mapped[int] = mapped_column(primary_key=True)

    Above, we may make use of Something.x_plus_y in a statement where it produces the full expression:

    1. >>> from sqlalchemy import select
    2. >>> print(select(Something.x_plus_y))
    3. SELECT something.x + something.y AS anon_1
    4. FROM something

    Tip

    New in version 2.0: - can accommodate a function decorated with @classmethod to help with PEP 484 integration where needed.

    When dealing with mapper inheritance patterns as documented at Mapping Class Inheritance Hierarchies, some additional capabilities are present when using either with mixin classes, or when augmenting both mapped and un-mapped superclasses in a class hierarchy.

    When defining functions decorated by declared_attr on mixins or base classes to be interpreted by subclasses in a mapped inheritance hierarchy, there is an important distinction made between functions that generate the special names used by Declarative such as __tablename__, __mapper_args__ vs. those that may generate ordinary mapped attributes such as and relationship(). Functions that define Declarative directives are invoked for each subclass in a hierarchy, whereas functions that generate mapped attributes are invoked only for the first mapped superclass in a hierarchy.

    The rationale for this difference in behavior is based on the fact that mapped properties are already inheritable by classes, such as a particular column on a superclass’ mapped table should not be duplicated to that of a subclass as well, whereas elements that are specific to a particular class or its mapped table are not inheritable, such as the name of the table that is locally mapped.

    The difference in behavior between these two use cases is demonstrated in the following two sections.

    A common recipe with mixins is to create a def __tablename__(cls) function that generates a name for the mapped Table dynamically.

    This recipe can be used to generate table names for an inheriting mapper hierarchy as in the example below which creates a mixin that gives every class a simple table name based on class name. The recipe is illustrated below where a table name is generated for the Person mapped class and the Engineer subclass of Person, but not for the Manager subclass of Person:

    1. from typing import Optional
    2. from sqlalchemy import ForeignKey
    3. from sqlalchemy.orm import DeclarativeBase
    4. from sqlalchemy.orm import declared_attr
    5. from sqlalchemy.orm import Mapped
    6. from sqlalchemy.orm import mapped_column
    7. class Base(DeclarativeBase):
    8. pass
    9. class Tablename:
    10. @declared_attr.directive
    11. def __tablename__(cls) -> Optional[str]:
    12. return cls.__name__.lower()
    13. class Person(Tablename, Base):
    14. id: Mapped[int] = mapped_column(primary_key=True)
    15. discriminator: Mapped[str]
    16. __mapper_args__ = {"polymorphic_on": "discriminator"}
    17. class Engineer(Person):
    18. id: Mapped[int] = mapped_column(ForeignKey("person.id"), primary_key=True)
    19. primary_language: Mapped[str]
    20. __mapper_args__ = {"polymorphic_identity": "engineer"}
    21. class Manager(Person):
    22. @declared_attr.directive
    23. def __tablename__(cls) -> Optional[str]:
    24. return None
    25. __mapper_args__ = {"polymorphic_identity": "manager"}

    In the above example, both the base class as well as the Engineer class, being subclasses of the Tablename mixin class which generates new table names, will have a generated __tablename__ attribute, which to Declarative indicates that each class should have its own generated to which it will be mapped. For the Engineer subclass, the style of inheritance applied is joined table inheritance, as it will be mapped to a table engineer that joins to the base person table. Any other subclasses that inherit from Person will also have this style of inheritance applied by default (and within this particular example, would need to each specify a primary key column; more on that in the next section).

    By contrast, the Manager subclass of Person overrides the __tablename__ classmethod to return None. This indicates to Declarative that this class should not have a generated, and will instead make use exclusively of the base Table to which Person is mapped. For the Manager subclass, the style of inheritance applied is .

    The example above illustrates that Declarative directives like __tablename__ are necessarily applied to each subclass individually, as each mapped class needs to state which Table it will be mapped towards, or if it will map itself to the inheriting superclass’ .

    If we instead wanted to reverse the default table scheme illustrated above, so that single table inheritance were the default and joined table inheritance could be defined only when a __tablename__ directive were supplied to override it, we can make use of Declarative helpers within the top-most __tablename__() method, in this case a helper called has_inherited_table(). This function will return True if a superclass is already mapped to a . We may use this helper within the base-most __tablename__() classmethod so that we may conditionally return None for the table name, if a table is already present, thus indicating single-table inheritance for inheriting subclasses by default:

    1. from sqlalchemy import ForeignKey
    2. from sqlalchemy.orm import DeclarativeBase
    3. from sqlalchemy.orm import declared_attr
    4. from sqlalchemy.orm import has_inherited_table
    5. from sqlalchemy.orm import Mapped
    6. from sqlalchemy.orm import mapped_column
    7. class Base(DeclarativeBase):
    8. pass
    9. class Tablename:
    10. @declared_attr.directive
    11. def __tablename__(cls):
    12. if has_inherited_table(cls):
    13. return None
    14. return cls.__name__.lower()
    15. class Person(Tablename, Base):
    16. id: Mapped[int] = mapped_column(primary_key=True)
    17. discriminator: Mapped[str]
    18. __mapper_args__ = {"polymorphic_on": "discriminator"}
    19. class Engineer(Person):
    20. @declared_attr.directive
    21. def __tablename__(cls):
    22. """override __tablename__ so that Engineer is joined-inheritance to Person"""
    23. return cls.__name__.lower()
    24. id: Mapped[int] = mapped_column(ForeignKey("person.id"), primary_key=True)
    25. primary_language: Mapped[str]
    26. __mapper_args__ = {"polymorphic_identity": "engineer"}
    27. class Manager(Person):
    28. __mapper_args__ = {"polymorphic_identity": "manager"}

    Using to generate table-specific inheriting columns

    In contrast to how __tablename__ and other special names are handled when used with declared_attr, when we mix in columns and properties (e.g. relationships, column properties, etc.), the function is invoked for the base class only in the hierarchy, unless the directive is used in combination with the declared_attr.cascading sub-directive. Below, only the Person class will receive a column called id; the mapping will fail on Engineer, which is not given a primary key:

    1. class HasId:
    2. id: Mapped[int] = mapped_column(primary_key=True)
    3. class Person(HasId, Base):
    4. __tablename__ = "person"
    5. discriminator: Mapped[str]
    6. __mapper_args__ = {"polymorphic_on": "discriminator"}
    7. # this mapping will fail, as there's no primary key
    8. class Engineer(Person):
    9. __tablename__ = "engineer"
    10. primary_language: Mapped[str]
    11. __mapper_args__ = {"polymorphic_identity": "engineer"}

    It is usually the case in joined-table inheritance that we want distinctly named columns on each subclass. However in this case, we may want to have an id column on every table, and have them refer to each other via foreign key. We can achieve this as a mixin by using the modifier, which indicates that the function should be invoked for each class in the hierarchy, in almost (see warning below) the same way as it does for __tablename__:

    1. class HasIdMixin:
    2. @declared_attr.cascading
    3. def id(cls) -> Mapped[int]:
    4. if has_inherited_table(cls):
    5. return mapped_column(ForeignKey("person.id"), primary_key=True)
    6. else:
    7. return mapped_column(Integer, primary_key=True)
    8. class Person(HasIdMixin, Base):
    9. __tablename__ = "person"
    10. discriminator: Mapped[str]
    11. __mapper_args__ = {"polymorphic_on": "discriminator"}
    12. class Engineer(Person):
    13. __tablename__ = "engineer"
    14. primary_language: Mapped[str]
    15. __mapper_args__ = {"polymorphic_identity": "engineer"}

    Warning

    The declared_attr.cascading feature currently does not allow for a subclass to override the attribute with a different function or value. This is a current limitation in the mechanics of how @declared_attr is resolved, and a warning is emitted if this condition is detected. This limitation only applies to ORM mapped columns, relationships, and other styles of attribute. It does not apply to Declarative directives such as __tablename__, __mapper_args__, etc., which resolve in a different way internally than that of declared_attr.cascading.

    Combining Table/Mapper Arguments from Multiple Mixins

    In the case of __table_args__ or __mapper_args__ specified with declarative mixins, you may want to combine some parameters from several mixins with those you wish to define on the class itself. The declared_attr decorator can be used here to create user-defined collation routines that pull from multiple collections:

    1. from sqlalchemy.orm import declarative_mixin, declared_attr
    2. class MySQLSettings:
    3. __table_args__ = {"mysql_engine": "InnoDB"}
    4. class MyOtherMixin:
    5. __table_args__ = {"info": "foo"}
    6. class MyModel(MySQLSettings, MyOtherMixin, Base):
    7. __tablename__ = "my_model"
    8. @declared_attr
    9. def __table_args__(cls):
    10. args = dict()
    11. args.update(MySQLSettings.__table_args__)
    12. args.update(MyOtherMixin.__table_args__)
    13. return args
    14. id = mapped_column(Integer, primary_key=True)

    To define a named, potentially multicolumn Index that applies to all tables derived from a mixin, use the “inline” form of and establish it as part of __table_args__:

    1. class MyMixin:
    2. a = mapped_column(Integer)
    3. b = mapped_column(Integer)
    4. @declared_attr
    5. def __table_args__(cls):
    6. return (Index(f"test_idx_{cls.__tablename__}", "a", "b"),)
    7. class MyModel(MyMixin, Base):
    8. __tablename__ = "atable"