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)
| 6031 | |
| 6032 | @property |
| 6033 | def sorted_tables(self) -> List[Table]: |
| 6034 | """Returns a list of :class:`_schema.Table` objects sorted in order of |
| 6035 | foreign key dependency. |
| 6036 | |
| 6037 | The sorting will place :class:`_schema.Table` |
| 6038 | objects that have dependencies |
| 6039 | first, before the dependencies themselves, representing the |
| 6040 | order in which they can be created. To get the order in which |
| 6041 | the tables would be dropped, use the ``reversed()`` Python built-in. |
| 6042 | |
| 6043 | .. warning:: |
| 6044 | |
| 6045 | The :attr:`.MetaData.sorted_tables` attribute cannot by itself |
| 6046 | accommodate automatic resolution of dependency cycles between |
| 6047 | tables, which are usually caused by mutually dependent foreign key |
| 6048 | constraints. When these cycles are detected, the foreign keys |
| 6049 | of these tables are omitted from consideration in the sort. |
| 6050 | A warning is emitted when this condition occurs, which will be an |
| 6051 | exception raise in a future release. Tables which are not part |
| 6052 | of the cycle will still be returned in dependency order. |
| 6053 | |
| 6054 | To resolve these cycles, the |
| 6055 | :paramref:`_schema.ForeignKeyConstraint.use_alter` parameter may be |
| 6056 | applied to those constraints which create a cycle. Alternatively, |
| 6057 | the :func:`_schema.sort_tables_and_constraints` function will |
| 6058 | automatically return foreign key constraints in a separate |
| 6059 | collection when cycles are detected so that they may be applied |
| 6060 | to a schema separately. |
| 6061 | |
| 6062 | .. seealso:: |
| 6063 | |
| 6064 | :func:`_schema.sort_tables` |
| 6065 | |
| 6066 | :func:`_schema.sort_tables_and_constraints` |
| 6067 | |
| 6068 | :attr:`_schema.MetaData.tables` |
| 6069 | |
| 6070 | :meth:`_reflection.Inspector.get_table_names` |
| 6071 | |
| 6072 | :meth:`_reflection.Inspector.get_sorted_table_and_fkc_names` |
| 6073 | |
| 6074 | |
| 6075 | """ |
| 6076 | return ddl.sort_tables( |
| 6077 | sorted(self.tables.values(), key=lambda t: t.key) # type: ignore |
| 6078 | ) |
| 6079 | |
| 6080 | # overload needed to work around mypy this mypy |
| 6081 | # https://github.com/python/mypy/issues/17093 |