Table Configuration with Declarative

As introduced at Declarative Mapping, the Declarative style includes the ability to generate a mapped Table object at the same time, or to accommodate a Table or other FromClause object directly.

The following examples assume a declarative base class as:

  1. from sqlalchemy.orm import declarative_base
  2. Base = declarative_base()

All of the examples that follow illustrate a class inheriting from the above Base. The decorator style introduced at Declarative Mapping using a Decorator (no declarative base) is fully supported with all the following examples as well.

Declarative Table

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

  1. from sqlalchemy import Column, Integer, String, ForeignKey
  2. from sqlalchemy.orm import declarative_base
  3. Base = declarative_base()
  4. class User(Base):
  5. __tablename__ = 'user'
  6. id = Column(Integer, primary_key=True)
  7. name = Column(String)
  8. fullname = Column(String)
  9. nickname = Column(String)

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

  1. # equivalent Table object produced
  2. user_table = Table(
  3. "user",
  4. Base.metadata,
  5. Column("id", Integer, primary_key=True),
  6. Column("name", String),
  7. Column("fullname", String),
  8. Column("nickname", String),
  9. )

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 Table available via the __table__ attribute:

  1. # access the Table
  2. user_table = User.__table__

The above table is ultimately the same one that corresponds to the Mapper.local_table attribute, which we can see through the runtime inspection system:

  1. from sqlalchemy import inspect
  2. user_table = inspect(User).local_table

The MetaData collection associated with both the declarative 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 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:

  1. engine = create_engine("sqlite://")
  2. Base.metadata.create_all(engine)

Declarative Table Configuration

When using Declarative Table configuration with the __tablename__ declarative class attribute, additional arguments to be supplied to the 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 Table constructor. The attribute can be specified in one of two forms. One is as a dictionary:

  1. class MyClass(Base):
  2. __tablename__ = 'sometable'
  3. __table_args__ = {'mysql_engine':'InnoDB'}

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

  1. class MyClass(Base):
  2. __tablename__ = 'sometable'
  3. __table_args__ = (
  4. ForeignKeyConstraint(['id'], ['remote_table.id']),
  5. UniqueConstraint('foo'),
  6. )

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

  1. class MyClass(Base):
  2. __tablename__ = 'sometable'
  3. __table_args__ = (
  4. ForeignKeyConstraint(['id'], ['remote_table.id']),
  5. UniqueConstraint('foo'),
  6. {'autoload':True}
  7. )

A class may also specify the __table_args__ declarative attribute, as well as the __tablename__ attribute, in a dynamic style using the declared_attr() method decorator. See the section Mixin and Custom Base Classes for examples on how this is often used.

Explicit Schema Name with Declarative Table

The schema name for a Table as documented at Specifying the Schema Name is applied to an individual Table using the Table.schema argument. When using Declarative tables, this option is passed like any other to the __table_args__ dictionary:

  1. class MyClass(Base):
  2. __tablename__ = 'sometable'
  3. __table_args__ = {'schema': 'some_schema'}

The schema name can also be applied to all Table objects globally by using the MetaData.schema parameter documented at Specifying a Default Schema Name with MetaData. The MetaData object may be constructed separately and passed either to registry() or declarative_base():

  1. from sqlalchemy import metadata
  2. metadata = MetaData(schema="some_schema")
  3. Base = declarative_base(metadata = metadata)
  4. class MyClass(Base):
  5. # will use "some_schema" by default
  6. __tablename__ = 'sometable'

See also

Specifying the Schema Name - in the Describing Databases with MetaData documentation.

Adding New Columns

The declarative table configuration allows the addition of new Column objects under two scenarios. The most basic is that of simply assigning new Column objects to the class:

  1. MyClass.some_new_column = Column('data', Unicode)

The above operation performed against a declarative class that has been mapped using the declarative base (note, not the decorator form of declarative) will add the above Column to the Table using the Table.append_column() method and will also add the column to the Mapper to be fully mapped.

Note

assignment of new columns to an existing declaratively mapped class will only function correctly if the “declarative base” class is used, which also provides for a metaclass-driven __setattr__() method which will intercept these operations. It will not work if the declarative decorator provided by registry.mapped() is used, nor will it work for an imperatively mapped class mapped by registry.map_imperatively().

The other scenario where a Column is added on the fly is when an inheriting subclass that has no table of its own indicates additional columns; these columns will be added to the superclass table. The section Single Table Inheritance discusses single table inheritance.

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

Declarative mappings may also be provided with a pre-existing Table object, or otherwise a Table or other arbitrary FromClause construct (such as a Join or 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 Table object is produced separately and passed to the declarative process directly:

  1. from sqlalchemy.orm import declarative_base
  2. from sqlalchemy import Column, Integer, String, ForeignKey
  3. Base = declarative_base()
  4. # construct a Table directly. The Base.metadata collection is
  5. # usually a good choice for MetaData but any MetaData
  6. # collection may be used.
  7. user_table = Table(
  8. "user",
  9. Base.metadata,
  10. Column("id", Integer, primary_key=True),
  11. Column("name", String),
  12. Column("fullname", String),
  13. Column("nickname", String),
  14. )
  15. # construct the User class using this table.
  16. class User(Base):
  17. __table__ = user_table

Above, a Table object is constructed using the approach described at Describing Databases with MetaData. 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:

  1. class User(Base):
  2. __table__ = Table(
  3. "user",
  4. Base.metadata,
  5. Column("id", Integer, primary_key=True),
  6. Column("name", String),
  7. Column("fullname", String),
  8. Column("nickname", String),
  9. )

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:

  1. class Person(Base):
  2. __table__ = Table(
  3. 'person',
  4. Base.metadata,
  5. Column('id', Integer, primary_key=True),
  6. Column('name', String(50)),
  7. Column('type', String(50))
  8. )
  9. __mapper_args__ = {
  10. "polymorphic_on": __table__.c.type,
  11. "polymorhpic_identity": "person"
  12. }

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

  1. from sqlalchemy import select, func
  2. subq = select(
  3. func.count(orders.c.id).label('order_count'),
  4. func.max(orders.c.price).label('highest_order'),
  5. orders.c.customer_id
  6. ).group_by(orders.c.customer_id).subquery()
  7. customer_select = select(customers, subq).join_from(
  8. customers, subq, customers.c.id == subq.c.customer_id
  9. ).subquery()
  10. class Customer(Base):
  11. __table__ = customer_select

For background on mapping to non-Table constructs see the sections Mapping a Class against Multiple Tables and Mapping a Class against 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 Declarative Mapping with Dataclasses and Attrs for detail.

See also

Describing Databases with MetaData

Declarative Mapping with Dataclasses and Attrs

Mapping Declaratively with Reflected Tables

There are several patterns available which provide for producing mapped classes against a series of Table objects that were introspected from the database, using the reflection process described at Reflecting Database Objects.

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

  1. engine = create_engine("postgresql://user:pass@hostname/my_existing_database")
  2. class MyClass(Base):
  3. __table__ = Table(
  4. 'mytable',
  5. Base.metadata,
  6. autoload_with=engine
  7. )

A major downside of the above approach however is that it 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.

Using DeferredReflection

To accommodate this case, a simple extension called the DeferredReflection mixin is available, which alters the declarative mapping process to be delayed until a special class-level 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:

  1. from sqlalchemy.orm import declarative_base
  2. from sqlalchemy.ext.declarative import DeferredReflection
  3. Base = declarative_base()
  4. class Reflected(DeferredReflection):
  5. __abstract__ = True
  6. class Foo(Reflected, Base):
  7. __tablename__ = 'foo'
  8. bars = relationship("Bar")
  9. class Bar(Reflected, Base):
  10. __tablename__ = 'bar'
  11. 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 Engine:

  1. engine = create_engine("postgresql://user:pass@hostname/my_existing_database")
  2. 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.

Using Automap

A more automated solution to mapping against an existing database where table reflection is to be used is to use the Automap extension. This extension will generate entire mapped classes from a database schema, and allows several hooks for customization including the ability to explicitly map some or all classes while still making use of reflection to fill in the remaining columns.

See also

Automap