module documentation

Provides the hierarchy of DDL-defining schema items as well as routines to invoke them for a create/drop call.
Class ​Add​Constraint Represent an ALTER TABLE ADD CONSTRAINT statement.
Class ​Create​Column Represent a _schema.Column as rendered in a CREATE TABLE statement, via the .CreateTable construct.
Class ​Create​Index Represent a CREATE INDEX statement.
Class ​Create​Schema Represent a CREATE SCHEMA statement.
Class ​Create​Sequence Represent a CREATE SEQUENCE statement.
Class ​Create​Table Represent a CREATE TABLE statement.
Class DDL A literal DDL statement.
Class ​DDLElement Base class for DDL expression constructs.
Class ​Drop​Constraint Represent an ALTER TABLE DROP CONSTRAINT statement.
Class ​Drop​Index Represent a DROP INDEX statement.
Class ​Drop​Schema Represent a DROP SCHEMA statement.
Class ​Drop​Sequence Represent a DROP SEQUENCE statement.
Class ​Drop​Table Represent a DROP TABLE statement.
Class _​Create​Drop​Base Base class for DDL constructs that represent CREATE and DROP or equivalents.
Class _​DDLCompiles No class docstring; 1/1 class variable, 1/2 method documented
Class _​Drop​View Semi-public 'DROP VIEW' construct.
Class ​DDLBase Undocumented
Class ​Drop​Column​Comment Represent a COMMENT ON COLUMN IS NULL statement.
Class ​Drop​Table​Comment Represent a COMMENT ON TABLE '' statement.
Class ​Schema​Dropper Undocumented
Class ​Schema​Generator Undocumented
Class ​Set​Column​Comment Represent a COMMENT ON COLUMN IS statement.
Class ​Set​Table​Comment Represent a COMMENT ON TABLE IS statement.
Function sort​_tables Sort a collection of _schema.Table objects based on dependency.
Function sort​_tables​_and​_constraints Sort a collection of _schema.Table / _schema.ForeignKeyConstraint objects.
def sort_tables(tables, skip_fn=None, extra_dependencies=None):

Sort a collection of _schema.Table objects based on dependency.

This is a dependency-ordered sort which will emit _schema.Table objects such that they will follow their dependent _schema.Table objects. Tables are dependent on another based on the presence of _schema.ForeignKeyConstraint objects as well as explicit dependencies added by _schema.Table.add_is_dependent_on.

Warning

The ._schema.sort_tables function cannot by itself accommodate automatic resolution of dependency cycles between tables, which are usually caused by mutually dependent foreign key constraints. When these cycles are detected, the foreign keys of these tables are omitted from consideration in the sort. A warning is emitted when this condition occurs, which will be an exception raise in a future release. Tables which are not part of the cycle will still be returned in dependency order.

To resolve these cycles, the :paramref:`_schema.ForeignKeyConstraint.use_alter` parameter may be applied to those constraints which create a cycle. Alternatively, the _schema.sort_tables_and_constraints function will automatically return foreign key constraints in a separate collection when cycles are detected so that they may be applied to a schema separately.

Changed in version 1.3.17: - a warning is emitted when _schema.sort_tables cannot perform a proper sort due to cyclical dependencies. This will be an exception in a future release. Additionally, the sort will continue to return other tables not involved in the cycle in dependency order which was not the case previously.

See Also

.sort_tables_and_constraints

_schema.MetaData.sorted_tables - uses this function to sort

Parameters
tablesa sequence of _schema.Table objects.
skip​_fnoptional callable which will be passed a _schema.ForeignKey object; if it returns True, this constraint will not be considered as a dependency. Note this is different from the same parameter in .sort_tables_and_constraints, which is instead passed the owning _schema.ForeignKeyConstraint object.
extra​_dependenciesa sequence of 2-tuples of tables which will also be considered as dependent on each other.
def sort_tables_and_constraints(tables, filter_fn=None, extra_dependencies=None, _warn_for_cycles=False):

Sort a collection of _schema.Table / _schema.ForeignKeyConstraint objects.

This is a dependency-ordered sort which will emit tuples of (Table, [ForeignKeyConstraint, ...]) such that each _schema.Table follows its dependent _schema.Table objects. Remaining _schema.ForeignKeyConstraint objects that are separate due to dependency rules not satisfied by the sort are emitted afterwards as (None, [ForeignKeyConstraint ...]).

Tables are dependent on another based on the presence of _schema.ForeignKeyConstraint objects, explicit dependencies added by _schema.Table.add_is_dependent_on, as well as dependencies stated here using the :paramref:`~.sort_tables_and_constraints.skip_fn` and/or :paramref:`~.sort_tables_and_constraints.extra_dependencies` parameters.

New in version 1.0.0.

See Also

.sort_tables

Parameters
tablesa sequence of _schema.Table objects.
filter​_fnoptional callable which will be passed a _schema.ForeignKeyConstraint object, and returns a value based on whether this constraint should definitely be included or excluded as an inline constraint, or neither. If it returns False, the constraint will definitely be included as a dependency that cannot be subject to ALTER; if True, it will only be included as an ALTER result at the end. Returning None means the constraint is included in the table-based result unless it is detected as part of a dependency cycle.
extra​_dependenciesa sequence of 2-tuples of tables which will also be considered as dependent on each other.
​_warn​_for​_cyclesUndocumented