MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / sorted_tables

Method sorted_tables

lib/sqlalchemy/sql/schema.py:5633–5685  ·  view source on GitHub ↗

Returns a list of :class:`_schema.Table` objects sorted in order of foreign key dependency. The sorting will place :class:`_schema.Table` objects that have dependencies first, before the dependencies themselves, representing the order in which they can be cre

(self)

Source from the content-addressed store, hash-verified

5631
5632 @property
5633 def sorted_tables(self) -> List[Table]:
5634 """Returns a list of :class:`_schema.Table` objects sorted in order of
5635 foreign key dependency.
5636
5637 The sorting will place :class:`_schema.Table`
5638 objects that have dependencies
5639 first, before the dependencies themselves, representing the
5640 order in which they can be created. To get the order in which
5641 the tables would be dropped, use the ``reversed()`` Python built-in.
5642
5643 .. warning::
5644
5645 The :attr:`.MetaData.sorted_tables` attribute cannot by itself
5646 accommodate automatic resolution of dependency cycles between
5647 tables, which are usually caused by mutually dependent foreign key
5648 constraints. When these cycles are detected, the foreign keys
5649 of these tables are omitted from consideration in the sort.
5650 A warning is emitted when this condition occurs, which will be an
5651 exception raise in a future release. Tables which are not part
5652 of the cycle will still be returned in dependency order.
5653
5654 To resolve these cycles, the
5655 :paramref:`_schema.ForeignKeyConstraint.use_alter` parameter may be
5656 applied to those constraints which create a cycle. Alternatively,
5657 the :func:`_schema.sort_tables_and_constraints` function will
5658 automatically return foreign key constraints in a separate
5659 collection when cycles are detected so that they may be applied
5660 to a schema separately.
5661
5662 .. versionchanged:: 1.3.17 - a warning is emitted when
5663 :attr:`.MetaData.sorted_tables` cannot perform a proper sort
5664 due to cyclical dependencies. This will be an exception in a
5665 future release. Additionally, the sort will continue to return
5666 other tables not involved in the cycle in dependency order which
5667 was not the case previously.
5668
5669 .. seealso::
5670
5671 :func:`_schema.sort_tables`
5672
5673 :func:`_schema.sort_tables_and_constraints`
5674
5675 :attr:`_schema.MetaData.tables`
5676
5677 :meth:`_reflection.Inspector.get_table_names`
5678
5679 :meth:`_reflection.Inspector.get_sorted_table_and_fkc_names`
5680
5681
5682 """
5683 return ddl.sort_tables(
5684 sorted(self.tables.values(), key=lambda t: t.key) # type: ignore
5685 )
5686
5687 # overload needed to work around mypy this mypy
5688 # https://github.com/python/mypy/issues/17093

Callers

nothing calls this directly

Calls 1

valuesMethod · 0.45

Tested by

no test coverage detected