ddl.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. # sql/ddl.py
  2. # Copyright (C) 2009-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: allow-untyped-defs, allow-untyped-calls
  8. """
  9. Provides the hierarchy of DDL-defining schema items as well as routines
  10. to invoke them for a create/drop call.
  11. """
  12. from __future__ import annotations
  13. import contextlib
  14. import typing
  15. from typing import Any
  16. from typing import Callable
  17. from typing import Generic
  18. from typing import Iterable
  19. from typing import List
  20. from typing import Optional
  21. from typing import Sequence as typing_Sequence
  22. from typing import Tuple
  23. from typing import TypeVar
  24. from typing import Union
  25. from . import roles
  26. from .base import _generative
  27. from .base import Executable
  28. from .base import SchemaVisitor
  29. from .elements import ClauseElement
  30. from .. import exc
  31. from .. import util
  32. from ..util import topological
  33. from ..util.typing import Protocol
  34. from ..util.typing import Self
  35. if typing.TYPE_CHECKING:
  36. from .compiler import Compiled
  37. from .compiler import DDLCompiler
  38. from .elements import BindParameter
  39. from .schema import Column
  40. from .schema import Constraint
  41. from .schema import ForeignKeyConstraint
  42. from .schema import Index
  43. from .schema import SchemaItem
  44. from .schema import Sequence as Sequence # noqa: F401
  45. from .schema import Table
  46. from .selectable import TableClause
  47. from ..engine.base import Connection
  48. from ..engine.interfaces import CacheStats
  49. from ..engine.interfaces import CompiledCacheType
  50. from ..engine.interfaces import Dialect
  51. from ..engine.interfaces import SchemaTranslateMapType
  52. _SI = TypeVar("_SI", bound=Union["SchemaItem", str])
  53. class BaseDDLElement(ClauseElement):
  54. """The root of DDL constructs, including those that are sub-elements
  55. within the "create table" and other processes.
  56. .. versionadded:: 2.0
  57. """
  58. _hierarchy_supports_caching = False
  59. """disable cache warnings for all _DDLCompiles subclasses. """
  60. def _compiler(self, dialect, **kw):
  61. """Return a compiler appropriate for this ClauseElement, given a
  62. Dialect."""
  63. return dialect.ddl_compiler(dialect, self, **kw)
  64. def _compile_w_cache(
  65. self,
  66. dialect: Dialect,
  67. *,
  68. compiled_cache: Optional[CompiledCacheType],
  69. column_keys: List[str],
  70. for_executemany: bool = False,
  71. schema_translate_map: Optional[SchemaTranslateMapType] = None,
  72. **kw: Any,
  73. ) -> Tuple[
  74. Compiled, Optional[typing_Sequence[BindParameter[Any]]], CacheStats
  75. ]:
  76. raise NotImplementedError()
  77. class DDLIfCallable(Protocol):
  78. def __call__(
  79. self,
  80. ddl: BaseDDLElement,
  81. target: Union[SchemaItem, str],
  82. bind: Optional[Connection],
  83. tables: Optional[List[Table]] = None,
  84. state: Optional[Any] = None,
  85. *,
  86. dialect: Dialect,
  87. compiler: Optional[DDLCompiler] = ...,
  88. checkfirst: bool,
  89. ) -> bool: ...
  90. class DDLIf(typing.NamedTuple):
  91. dialect: Optional[str]
  92. callable_: Optional[DDLIfCallable]
  93. state: Optional[Any]
  94. def _should_execute(
  95. self,
  96. ddl: BaseDDLElement,
  97. target: Union[SchemaItem, str],
  98. bind: Optional[Connection],
  99. compiler: Optional[DDLCompiler] = None,
  100. **kw: Any,
  101. ) -> bool:
  102. if bind is not None:
  103. dialect = bind.dialect
  104. elif compiler is not None:
  105. dialect = compiler.dialect
  106. else:
  107. assert False, "compiler or dialect is required"
  108. if isinstance(self.dialect, str):
  109. if self.dialect != dialect.name:
  110. return False
  111. elif isinstance(self.dialect, (tuple, list, set)):
  112. if dialect.name not in self.dialect:
  113. return False
  114. if self.callable_ is not None and not self.callable_(
  115. ddl,
  116. target,
  117. bind,
  118. state=self.state,
  119. dialect=dialect,
  120. compiler=compiler,
  121. **kw,
  122. ):
  123. return False
  124. return True
  125. class ExecutableDDLElement(roles.DDLRole, Executable, BaseDDLElement):
  126. """Base class for standalone executable DDL expression constructs.
  127. This class is the base for the general purpose :class:`.DDL` class,
  128. as well as the various create/drop clause constructs such as
  129. :class:`.CreateTable`, :class:`.DropTable`, :class:`.AddConstraint`,
  130. etc.
  131. .. versionchanged:: 2.0 :class:`.ExecutableDDLElement` is renamed from
  132. :class:`.DDLElement`, which still exists for backwards compatibility.
  133. :class:`.ExecutableDDLElement` integrates closely with SQLAlchemy events,
  134. introduced in :ref:`event_toplevel`. An instance of one is
  135. itself an event receiving callable::
  136. event.listen(
  137. users,
  138. "after_create",
  139. AddConstraint(constraint).execute_if(dialect="postgresql"),
  140. )
  141. .. seealso::
  142. :class:`.DDL`
  143. :class:`.DDLEvents`
  144. :ref:`event_toplevel`
  145. :ref:`schema_ddl_sequences`
  146. """
  147. _ddl_if: Optional[DDLIf] = None
  148. target: Union[SchemaItem, str, None] = None
  149. def _execute_on_connection(
  150. self, connection, distilled_params, execution_options
  151. ):
  152. return connection._execute_ddl(
  153. self, distilled_params, execution_options
  154. )
  155. @_generative
  156. def against(self, target: SchemaItem) -> Self:
  157. """Return a copy of this :class:`_schema.ExecutableDDLElement` which
  158. will include the given target.
  159. This essentially applies the given item to the ``.target`` attribute of
  160. the returned :class:`_schema.ExecutableDDLElement` object. This target
  161. is then usable by event handlers and compilation routines in order to
  162. provide services such as tokenization of a DDL string in terms of a
  163. particular :class:`_schema.Table`.
  164. When a :class:`_schema.ExecutableDDLElement` object is established as
  165. an event handler for the :meth:`_events.DDLEvents.before_create` or
  166. :meth:`_events.DDLEvents.after_create` events, and the event then
  167. occurs for a given target such as a :class:`_schema.Constraint` or
  168. :class:`_schema.Table`, that target is established with a copy of the
  169. :class:`_schema.ExecutableDDLElement` object using this method, which
  170. then proceeds to the :meth:`_schema.ExecutableDDLElement.execute`
  171. method in order to invoke the actual DDL instruction.
  172. :param target: a :class:`_schema.SchemaItem` that will be the subject
  173. of a DDL operation.
  174. :return: a copy of this :class:`_schema.ExecutableDDLElement` with the
  175. ``.target`` attribute assigned to the given
  176. :class:`_schema.SchemaItem`.
  177. .. seealso::
  178. :class:`_schema.DDL` - uses tokenization against the "target" when
  179. processing the DDL string.
  180. """
  181. self.target = target
  182. return self
  183. @_generative
  184. def execute_if(
  185. self,
  186. dialect: Optional[str] = None,
  187. callable_: Optional[DDLIfCallable] = None,
  188. state: Optional[Any] = None,
  189. ) -> Self:
  190. r"""Return a callable that will execute this
  191. :class:`_ddl.ExecutableDDLElement` conditionally within an event
  192. handler.
  193. Used to provide a wrapper for event listening::
  194. event.listen(
  195. metadata,
  196. "before_create",
  197. DDL("my_ddl").execute_if(dialect="postgresql"),
  198. )
  199. :param dialect: May be a string or tuple of strings.
  200. If a string, it will be compared to the name of the
  201. executing database dialect::
  202. DDL("something").execute_if(dialect="postgresql")
  203. If a tuple, specifies multiple dialect names::
  204. DDL("something").execute_if(dialect=("postgresql", "mysql"))
  205. :param callable\_: A callable, which will be invoked with
  206. three positional arguments as well as optional keyword
  207. arguments:
  208. :ddl:
  209. This DDL element.
  210. :target:
  211. The :class:`_schema.Table` or :class:`_schema.MetaData`
  212. object which is the
  213. target of this event. May be None if the DDL is executed
  214. explicitly.
  215. :bind:
  216. The :class:`_engine.Connection` being used for DDL execution.
  217. May be None if this construct is being created inline within
  218. a table, in which case ``compiler`` will be present.
  219. :tables:
  220. Optional keyword argument - a list of Table objects which are to
  221. be created/ dropped within a MetaData.create_all() or drop_all()
  222. method call.
  223. :dialect: keyword argument, but always present - the
  224. :class:`.Dialect` involved in the operation.
  225. :compiler: keyword argument. Will be ``None`` for an engine
  226. level DDL invocation, but will refer to a :class:`.DDLCompiler`
  227. if this DDL element is being created inline within a table.
  228. :state:
  229. Optional keyword argument - will be the ``state`` argument
  230. passed to this function.
  231. :checkfirst:
  232. Keyword argument, will be True if the 'checkfirst' flag was
  233. set during the call to ``create()``, ``create_all()``,
  234. ``drop()``, ``drop_all()``.
  235. If the callable returns a True value, the DDL statement will be
  236. executed.
  237. :param state: any value which will be passed to the callable\_
  238. as the ``state`` keyword argument.
  239. .. seealso::
  240. :meth:`.SchemaItem.ddl_if`
  241. :class:`.DDLEvents`
  242. :ref:`event_toplevel`
  243. """
  244. self._ddl_if = DDLIf(dialect, callable_, state)
  245. return self
  246. def _should_execute(self, target, bind, **kw):
  247. if self._ddl_if is None:
  248. return True
  249. else:
  250. return self._ddl_if._should_execute(self, target, bind, **kw)
  251. def _invoke_with(self, bind):
  252. if self._should_execute(self.target, bind):
  253. return bind.execute(self)
  254. def __call__(self, target, bind, **kw):
  255. """Execute the DDL as a ddl_listener."""
  256. self.against(target)._invoke_with(bind)
  257. def _generate(self):
  258. s = self.__class__.__new__(self.__class__)
  259. s.__dict__ = self.__dict__.copy()
  260. return s
  261. DDLElement = ExecutableDDLElement
  262. """:class:`.DDLElement` is renamed to :class:`.ExecutableDDLElement`."""
  263. class DDL(ExecutableDDLElement):
  264. """A literal DDL statement.
  265. Specifies literal SQL DDL to be executed by the database. DDL objects
  266. function as DDL event listeners, and can be subscribed to those events
  267. listed in :class:`.DDLEvents`, using either :class:`_schema.Table` or
  268. :class:`_schema.MetaData` objects as targets.
  269. Basic templating support allows
  270. a single DDL instance to handle repetitive tasks for multiple tables.
  271. Examples::
  272. from sqlalchemy import event, DDL
  273. tbl = Table("users", metadata, Column("uid", Integer))
  274. event.listen(tbl, "before_create", DDL("DROP TRIGGER users_trigger"))
  275. spow = DDL("ALTER TABLE %(table)s SET secretpowers TRUE")
  276. event.listen(tbl, "after_create", spow.execute_if(dialect="somedb"))
  277. drop_spow = DDL("ALTER TABLE users SET secretpowers FALSE")
  278. connection.execute(drop_spow)
  279. When operating on Table events, the following ``statement``
  280. string substitutions are available:
  281. .. sourcecode:: text
  282. %(table)s - the Table name, with any required quoting applied
  283. %(schema)s - the schema name, with any required quoting applied
  284. %(fullname)s - the Table name including schema, quoted if needed
  285. The DDL's "context", if any, will be combined with the standard
  286. substitutions noted above. Keys present in the context will override
  287. the standard substitutions.
  288. """
  289. __visit_name__ = "ddl"
  290. def __init__(self, statement, context=None):
  291. """Create a DDL statement.
  292. :param statement:
  293. A string or unicode string to be executed. Statements will be
  294. processed with Python's string formatting operator using
  295. a fixed set of string substitutions, as well as additional
  296. substitutions provided by the optional :paramref:`.DDL.context`
  297. parameter.
  298. A literal '%' in a statement must be escaped as '%%'.
  299. SQL bind parameters are not available in DDL statements.
  300. :param context:
  301. Optional dictionary, defaults to None. These values will be
  302. available for use in string substitutions on the DDL statement.
  303. .. seealso::
  304. :class:`.DDLEvents`
  305. :ref:`event_toplevel`
  306. """
  307. if not isinstance(statement, str):
  308. raise exc.ArgumentError(
  309. "Expected a string or unicode SQL statement, got '%r'"
  310. % statement
  311. )
  312. self.statement = statement
  313. self.context = context or {}
  314. def __repr__(self):
  315. parts = [repr(self.statement)]
  316. if self.context:
  317. parts.append(f"context={self.context}")
  318. return "<%s@%s; %s>" % (
  319. type(self).__name__,
  320. id(self),
  321. ", ".join(parts),
  322. )
  323. class _CreateDropBase(ExecutableDDLElement, Generic[_SI]):
  324. """Base class for DDL constructs that represent CREATE and DROP or
  325. equivalents.
  326. The common theme of _CreateDropBase is a single
  327. ``element`` attribute which refers to the element
  328. to be created or dropped.
  329. """
  330. element: _SI
  331. def __init__(self, element: _SI) -> None:
  332. self.element = self.target = element
  333. self._ddl_if = getattr(element, "_ddl_if", None)
  334. @property
  335. def stringify_dialect(self): # type: ignore[override]
  336. assert not isinstance(self.element, str)
  337. return self.element.create_drop_stringify_dialect
  338. def _create_rule_disable(self, compiler):
  339. """Allow disable of _create_rule using a callable.
  340. Pass to _create_rule using
  341. util.portable_instancemethod(self._create_rule_disable)
  342. to retain serializability.
  343. """
  344. return False
  345. class _CreateBase(_CreateDropBase[_SI]):
  346. def __init__(self, element: _SI, if_not_exists: bool = False) -> None:
  347. super().__init__(element)
  348. self.if_not_exists = if_not_exists
  349. class _DropBase(_CreateDropBase[_SI]):
  350. def __init__(self, element: _SI, if_exists: bool = False) -> None:
  351. super().__init__(element)
  352. self.if_exists = if_exists
  353. class CreateSchema(_CreateBase[str]):
  354. """Represent a CREATE SCHEMA statement.
  355. The argument here is the string name of the schema.
  356. """
  357. __visit_name__ = "create_schema"
  358. stringify_dialect = "default"
  359. def __init__(
  360. self,
  361. name: str,
  362. if_not_exists: bool = False,
  363. ) -> None:
  364. """Create a new :class:`.CreateSchema` construct."""
  365. super().__init__(element=name, if_not_exists=if_not_exists)
  366. class DropSchema(_DropBase[str]):
  367. """Represent a DROP SCHEMA statement.
  368. The argument here is the string name of the schema.
  369. """
  370. __visit_name__ = "drop_schema"
  371. stringify_dialect = "default"
  372. def __init__(
  373. self,
  374. name: str,
  375. cascade: bool = False,
  376. if_exists: bool = False,
  377. ) -> None:
  378. """Create a new :class:`.DropSchema` construct."""
  379. super().__init__(element=name, if_exists=if_exists)
  380. self.cascade = cascade
  381. class CreateTable(_CreateBase["Table"]):
  382. """Represent a CREATE TABLE statement."""
  383. __visit_name__ = "create_table"
  384. def __init__(
  385. self,
  386. element: Table,
  387. include_foreign_key_constraints: Optional[
  388. typing_Sequence[ForeignKeyConstraint]
  389. ] = None,
  390. if_not_exists: bool = False,
  391. ) -> None:
  392. """Create a :class:`.CreateTable` construct.
  393. :param element: a :class:`_schema.Table` that's the subject
  394. of the CREATE
  395. :param on: See the description for 'on' in :class:`.DDL`.
  396. :param include_foreign_key_constraints: optional sequence of
  397. :class:`_schema.ForeignKeyConstraint` objects that will be included
  398. inline within the CREATE construct; if omitted, all foreign key
  399. constraints that do not specify use_alter=True are included.
  400. :param if_not_exists: if True, an IF NOT EXISTS operator will be
  401. applied to the construct.
  402. .. versionadded:: 1.4.0b2
  403. """
  404. super().__init__(element, if_not_exists=if_not_exists)
  405. self.columns = [CreateColumn(column) for column in element.columns]
  406. self.include_foreign_key_constraints = include_foreign_key_constraints
  407. class _DropView(_DropBase["Table"]):
  408. """Semi-public 'DROP VIEW' construct.
  409. Used by the test suite for dialect-agnostic drops of views.
  410. This object will eventually be part of a public "view" API.
  411. """
  412. __visit_name__ = "drop_view"
  413. class CreateConstraint(BaseDDLElement):
  414. element: Constraint
  415. def __init__(self, element: Constraint) -> None:
  416. self.element = element
  417. class CreateColumn(BaseDDLElement):
  418. """Represent a :class:`_schema.Column`
  419. as rendered in a CREATE TABLE statement,
  420. via the :class:`.CreateTable` construct.
  421. This is provided to support custom column DDL within the generation
  422. of CREATE TABLE statements, by using the
  423. compiler extension documented in :ref:`sqlalchemy.ext.compiler_toplevel`
  424. to extend :class:`.CreateColumn`.
  425. Typical integration is to examine the incoming :class:`_schema.Column`
  426. object, and to redirect compilation if a particular flag or condition
  427. is found::
  428. from sqlalchemy import schema
  429. from sqlalchemy.ext.compiler import compiles
  430. @compiles(schema.CreateColumn)
  431. def compile(element, compiler, **kw):
  432. column = element.element
  433. if "special" not in column.info:
  434. return compiler.visit_create_column(element, **kw)
  435. text = "%s SPECIAL DIRECTIVE %s" % (
  436. column.name,
  437. compiler.type_compiler.process(column.type),
  438. )
  439. default = compiler.get_column_default_string(column)
  440. if default is not None:
  441. text += " DEFAULT " + default
  442. if not column.nullable:
  443. text += " NOT NULL"
  444. if column.constraints:
  445. text += " ".join(
  446. compiler.process(const) for const in column.constraints
  447. )
  448. return text
  449. The above construct can be applied to a :class:`_schema.Table`
  450. as follows::
  451. from sqlalchemy import Table, Metadata, Column, Integer, String
  452. from sqlalchemy import schema
  453. metadata = MetaData()
  454. table = Table(
  455. "mytable",
  456. MetaData(),
  457. Column("x", Integer, info={"special": True}, primary_key=True),
  458. Column("y", String(50)),
  459. Column("z", String(20), info={"special": True}),
  460. )
  461. metadata.create_all(conn)
  462. Above, the directives we've added to the :attr:`_schema.Column.info`
  463. collection
  464. will be detected by our custom compilation scheme:
  465. .. sourcecode:: sql
  466. CREATE TABLE mytable (
  467. x SPECIAL DIRECTIVE INTEGER NOT NULL,
  468. y VARCHAR(50),
  469. z SPECIAL DIRECTIVE VARCHAR(20),
  470. PRIMARY KEY (x)
  471. )
  472. The :class:`.CreateColumn` construct can also be used to skip certain
  473. columns when producing a ``CREATE TABLE``. This is accomplished by
  474. creating a compilation rule that conditionally returns ``None``.
  475. This is essentially how to produce the same effect as using the
  476. ``system=True`` argument on :class:`_schema.Column`, which marks a column
  477. as an implicitly-present "system" column.
  478. For example, suppose we wish to produce a :class:`_schema.Table`
  479. which skips
  480. rendering of the PostgreSQL ``xmin`` column against the PostgreSQL
  481. backend, but on other backends does render it, in anticipation of a
  482. triggered rule. A conditional compilation rule could skip this name only
  483. on PostgreSQL::
  484. from sqlalchemy.schema import CreateColumn
  485. @compiles(CreateColumn, "postgresql")
  486. def skip_xmin(element, compiler, **kw):
  487. if element.element.name == "xmin":
  488. return None
  489. else:
  490. return compiler.visit_create_column(element, **kw)
  491. my_table = Table(
  492. "mytable",
  493. metadata,
  494. Column("id", Integer, primary_key=True),
  495. Column("xmin", Integer),
  496. )
  497. Above, a :class:`.CreateTable` construct will generate a ``CREATE TABLE``
  498. which only includes the ``id`` column in the string; the ``xmin`` column
  499. will be omitted, but only against the PostgreSQL backend.
  500. """
  501. __visit_name__ = "create_column"
  502. element: Column[Any]
  503. def __init__(self, element: Column[Any]) -> None:
  504. self.element = element
  505. class DropTable(_DropBase["Table"]):
  506. """Represent a DROP TABLE statement."""
  507. __visit_name__ = "drop_table"
  508. def __init__(self, element: Table, if_exists: bool = False) -> None:
  509. """Create a :class:`.DropTable` construct.
  510. :param element: a :class:`_schema.Table` that's the subject
  511. of the DROP.
  512. :param on: See the description for 'on' in :class:`.DDL`.
  513. :param if_exists: if True, an IF EXISTS operator will be applied to the
  514. construct.
  515. .. versionadded:: 1.4.0b2
  516. """
  517. super().__init__(element, if_exists=if_exists)
  518. class CreateSequence(_CreateBase["Sequence"]):
  519. """Represent a CREATE SEQUENCE statement."""
  520. __visit_name__ = "create_sequence"
  521. class DropSequence(_DropBase["Sequence"]):
  522. """Represent a DROP SEQUENCE statement."""
  523. __visit_name__ = "drop_sequence"
  524. class CreateIndex(_CreateBase["Index"]):
  525. """Represent a CREATE INDEX statement."""
  526. __visit_name__ = "create_index"
  527. def __init__(self, element: Index, if_not_exists: bool = False) -> None:
  528. """Create a :class:`.Createindex` construct.
  529. :param element: a :class:`_schema.Index` that's the subject
  530. of the CREATE.
  531. :param if_not_exists: if True, an IF NOT EXISTS operator will be
  532. applied to the construct.
  533. .. versionadded:: 1.4.0b2
  534. """
  535. super().__init__(element, if_not_exists=if_not_exists)
  536. class DropIndex(_DropBase["Index"]):
  537. """Represent a DROP INDEX statement."""
  538. __visit_name__ = "drop_index"
  539. def __init__(self, element: Index, if_exists: bool = False) -> None:
  540. """Create a :class:`.DropIndex` construct.
  541. :param element: a :class:`_schema.Index` that's the subject
  542. of the DROP.
  543. :param if_exists: if True, an IF EXISTS operator will be applied to the
  544. construct.
  545. .. versionadded:: 1.4.0b2
  546. """
  547. super().__init__(element, if_exists=if_exists)
  548. class AddConstraint(_CreateBase["Constraint"]):
  549. """Represent an ALTER TABLE ADD CONSTRAINT statement."""
  550. __visit_name__ = "add_constraint"
  551. def __init__(
  552. self,
  553. element: Constraint,
  554. *,
  555. isolate_from_table: bool = True,
  556. ) -> None:
  557. """Construct a new :class:`.AddConstraint` construct.
  558. :param element: a :class:`.Constraint` object
  559. :param isolate_from_table: optional boolean, defaults to True. Has
  560. the effect of the incoming constraint being isolated from being
  561. included in a CREATE TABLE sequence when associated with a
  562. :class:`.Table`.
  563. .. versionadded:: 2.0.39 - added
  564. :paramref:`.AddConstraint.isolate_from_table`, defaulting
  565. to True. Previously, the behavior of this parameter was implicitly
  566. turned on in all cases.
  567. """
  568. super().__init__(element)
  569. if isolate_from_table:
  570. element._create_rule = util.portable_instancemethod(
  571. self._create_rule_disable
  572. )
  573. class DropConstraint(_DropBase["Constraint"]):
  574. """Represent an ALTER TABLE DROP CONSTRAINT statement."""
  575. __visit_name__ = "drop_constraint"
  576. def __init__(
  577. self,
  578. element: Constraint,
  579. *,
  580. cascade: bool = False,
  581. if_exists: bool = False,
  582. isolate_from_table: bool = True,
  583. **kw: Any,
  584. ) -> None:
  585. """Construct a new :class:`.DropConstraint` construct.
  586. :param element: a :class:`.Constraint` object
  587. :param cascade: optional boolean, indicates backend-specific
  588. "CASCADE CONSTRAINT" directive should be rendered if available
  589. :param if_exists: optional boolean, indicates backend-specific
  590. "IF EXISTS" directive should be rendered if available
  591. :param isolate_from_table: optional boolean, defaults to True. Has
  592. the effect of the incoming constraint being isolated from being
  593. included in a CREATE TABLE sequence when associated with a
  594. :class:`.Table`.
  595. .. versionadded:: 2.0.39 - added
  596. :paramref:`.DropConstraint.isolate_from_table`, defaulting
  597. to True. Previously, the behavior of this parameter was implicitly
  598. turned on in all cases.
  599. """
  600. self.cascade = cascade
  601. super().__init__(element, if_exists=if_exists, **kw)
  602. if isolate_from_table:
  603. element._create_rule = util.portable_instancemethod(
  604. self._create_rule_disable
  605. )
  606. class SetTableComment(_CreateDropBase["Table"]):
  607. """Represent a COMMENT ON TABLE IS statement."""
  608. __visit_name__ = "set_table_comment"
  609. class DropTableComment(_CreateDropBase["Table"]):
  610. """Represent a COMMENT ON TABLE '' statement.
  611. Note this varies a lot across database backends.
  612. """
  613. __visit_name__ = "drop_table_comment"
  614. class SetColumnComment(_CreateDropBase["Column[Any]"]):
  615. """Represent a COMMENT ON COLUMN IS statement."""
  616. __visit_name__ = "set_column_comment"
  617. class DropColumnComment(_CreateDropBase["Column[Any]"]):
  618. """Represent a COMMENT ON COLUMN IS NULL statement."""
  619. __visit_name__ = "drop_column_comment"
  620. class SetConstraintComment(_CreateDropBase["Constraint"]):
  621. """Represent a COMMENT ON CONSTRAINT IS statement."""
  622. __visit_name__ = "set_constraint_comment"
  623. class DropConstraintComment(_CreateDropBase["Constraint"]):
  624. """Represent a COMMENT ON CONSTRAINT IS NULL statement."""
  625. __visit_name__ = "drop_constraint_comment"
  626. class InvokeDDLBase(SchemaVisitor):
  627. def __init__(self, connection, **kw):
  628. self.connection = connection
  629. assert not kw, f"Unexpected keywords: {kw.keys()}"
  630. @contextlib.contextmanager
  631. def with_ddl_events(self, target, **kw):
  632. """helper context manager that will apply appropriate DDL events
  633. to a CREATE or DROP operation."""
  634. raise NotImplementedError()
  635. class InvokeCreateDDLBase(InvokeDDLBase):
  636. @contextlib.contextmanager
  637. def with_ddl_events(self, target, **kw):
  638. """helper context manager that will apply appropriate DDL events
  639. to a CREATE or DROP operation."""
  640. target.dispatch.before_create(
  641. target, self.connection, _ddl_runner=self, **kw
  642. )
  643. yield
  644. target.dispatch.after_create(
  645. target, self.connection, _ddl_runner=self, **kw
  646. )
  647. class InvokeDropDDLBase(InvokeDDLBase):
  648. @contextlib.contextmanager
  649. def with_ddl_events(self, target, **kw):
  650. """helper context manager that will apply appropriate DDL events
  651. to a CREATE or DROP operation."""
  652. target.dispatch.before_drop(
  653. target, self.connection, _ddl_runner=self, **kw
  654. )
  655. yield
  656. target.dispatch.after_drop(
  657. target, self.connection, _ddl_runner=self, **kw
  658. )
  659. class SchemaGenerator(InvokeCreateDDLBase):
  660. def __init__(
  661. self, dialect, connection, checkfirst=False, tables=None, **kwargs
  662. ):
  663. super().__init__(connection, **kwargs)
  664. self.checkfirst = checkfirst
  665. self.tables = tables
  666. self.preparer = dialect.identifier_preparer
  667. self.dialect = dialect
  668. self.memo = {}
  669. def _can_create_table(self, table):
  670. self.dialect.validate_identifier(table.name)
  671. effective_schema = self.connection.schema_for_object(table)
  672. if effective_schema:
  673. self.dialect.validate_identifier(effective_schema)
  674. return not self.checkfirst or not self.dialect.has_table(
  675. self.connection, table.name, schema=effective_schema
  676. )
  677. def _can_create_index(self, index):
  678. effective_schema = self.connection.schema_for_object(index.table)
  679. if effective_schema:
  680. self.dialect.validate_identifier(effective_schema)
  681. return not self.checkfirst or not self.dialect.has_index(
  682. self.connection,
  683. index.table.name,
  684. index.name,
  685. schema=effective_schema,
  686. )
  687. def _can_create_sequence(self, sequence):
  688. effective_schema = self.connection.schema_for_object(sequence)
  689. return self.dialect.supports_sequences and (
  690. (not self.dialect.sequences_optional or not sequence.optional)
  691. and (
  692. not self.checkfirst
  693. or not self.dialect.has_sequence(
  694. self.connection, sequence.name, schema=effective_schema
  695. )
  696. )
  697. )
  698. def visit_metadata(self, metadata):
  699. if self.tables is not None:
  700. tables = self.tables
  701. else:
  702. tables = list(metadata.tables.values())
  703. collection = sort_tables_and_constraints(
  704. [t for t in tables if self._can_create_table(t)]
  705. )
  706. seq_coll = [
  707. s
  708. for s in metadata._sequences.values()
  709. if s.column is None and self._can_create_sequence(s)
  710. ]
  711. event_collection = [t for (t, fks) in collection if t is not None]
  712. with self.with_ddl_events(
  713. metadata,
  714. tables=event_collection,
  715. checkfirst=self.checkfirst,
  716. ):
  717. for seq in seq_coll:
  718. self.traverse_single(seq, create_ok=True)
  719. for table, fkcs in collection:
  720. if table is not None:
  721. self.traverse_single(
  722. table,
  723. create_ok=True,
  724. include_foreign_key_constraints=fkcs,
  725. _is_metadata_operation=True,
  726. )
  727. else:
  728. for fkc in fkcs:
  729. self.traverse_single(fkc)
  730. def visit_table(
  731. self,
  732. table,
  733. create_ok=False,
  734. include_foreign_key_constraints=None,
  735. _is_metadata_operation=False,
  736. ):
  737. if not create_ok and not self._can_create_table(table):
  738. return
  739. with self.with_ddl_events(
  740. table,
  741. checkfirst=self.checkfirst,
  742. _is_metadata_operation=_is_metadata_operation,
  743. ):
  744. for column in table.columns:
  745. if column.default is not None:
  746. self.traverse_single(column.default)
  747. if not self.dialect.supports_alter:
  748. # e.g., don't omit any foreign key constraints
  749. include_foreign_key_constraints = None
  750. CreateTable(
  751. table,
  752. include_foreign_key_constraints=(
  753. include_foreign_key_constraints
  754. ),
  755. )._invoke_with(self.connection)
  756. if hasattr(table, "indexes"):
  757. for index in table.indexes:
  758. self.traverse_single(index, create_ok=True)
  759. if (
  760. self.dialect.supports_comments
  761. and not self.dialect.inline_comments
  762. ):
  763. if table.comment is not None:
  764. SetTableComment(table)._invoke_with(self.connection)
  765. for column in table.columns:
  766. if column.comment is not None:
  767. SetColumnComment(column)._invoke_with(self.connection)
  768. if self.dialect.supports_constraint_comments:
  769. for constraint in table.constraints:
  770. if constraint.comment is not None:
  771. self.connection.execute(
  772. SetConstraintComment(constraint)
  773. )
  774. def visit_foreign_key_constraint(self, constraint):
  775. if not self.dialect.supports_alter:
  776. return
  777. with self.with_ddl_events(constraint):
  778. AddConstraint(constraint)._invoke_with(self.connection)
  779. def visit_sequence(self, sequence, create_ok=False):
  780. if not create_ok and not self._can_create_sequence(sequence):
  781. return
  782. with self.with_ddl_events(sequence):
  783. CreateSequence(sequence)._invoke_with(self.connection)
  784. def visit_index(self, index, create_ok=False):
  785. if not create_ok and not self._can_create_index(index):
  786. return
  787. with self.with_ddl_events(index):
  788. CreateIndex(index)._invoke_with(self.connection)
  789. class SchemaDropper(InvokeDropDDLBase):
  790. def __init__(
  791. self, dialect, connection, checkfirst=False, tables=None, **kwargs
  792. ):
  793. super().__init__(connection, **kwargs)
  794. self.checkfirst = checkfirst
  795. self.tables = tables
  796. self.preparer = dialect.identifier_preparer
  797. self.dialect = dialect
  798. self.memo = {}
  799. def visit_metadata(self, metadata):
  800. if self.tables is not None:
  801. tables = self.tables
  802. else:
  803. tables = list(metadata.tables.values())
  804. try:
  805. unsorted_tables = [t for t in tables if self._can_drop_table(t)]
  806. collection = list(
  807. reversed(
  808. sort_tables_and_constraints(
  809. unsorted_tables,
  810. filter_fn=lambda constraint: (
  811. False
  812. if not self.dialect.supports_alter
  813. or constraint.name is None
  814. else None
  815. ),
  816. )
  817. )
  818. )
  819. except exc.CircularDependencyError as err2:
  820. if not self.dialect.supports_alter:
  821. util.warn(
  822. "Can't sort tables for DROP; an "
  823. "unresolvable foreign key "
  824. "dependency exists between tables: %s; and backend does "
  825. "not support ALTER. To restore at least a partial sort, "
  826. "apply use_alter=True to ForeignKey and "
  827. "ForeignKeyConstraint "
  828. "objects involved in the cycle to mark these as known "
  829. "cycles that will be ignored."
  830. % (", ".join(sorted([t.fullname for t in err2.cycles])))
  831. )
  832. collection = [(t, ()) for t in unsorted_tables]
  833. else:
  834. raise exc.CircularDependencyError(
  835. err2.args[0],
  836. err2.cycles,
  837. err2.edges,
  838. msg="Can't sort tables for DROP; an "
  839. "unresolvable foreign key "
  840. "dependency exists between tables: %s. Please ensure "
  841. "that the ForeignKey and ForeignKeyConstraint objects "
  842. "involved in the cycle have "
  843. "names so that they can be dropped using "
  844. "DROP CONSTRAINT."
  845. % (", ".join(sorted([t.fullname for t in err2.cycles]))),
  846. ) from err2
  847. seq_coll = [
  848. s
  849. for s in metadata._sequences.values()
  850. if self._can_drop_sequence(s)
  851. ]
  852. event_collection = [t for (t, fks) in collection if t is not None]
  853. with self.with_ddl_events(
  854. metadata,
  855. tables=event_collection,
  856. checkfirst=self.checkfirst,
  857. ):
  858. for table, fkcs in collection:
  859. if table is not None:
  860. self.traverse_single(
  861. table,
  862. drop_ok=True,
  863. _is_metadata_operation=True,
  864. _ignore_sequences=seq_coll,
  865. )
  866. else:
  867. for fkc in fkcs:
  868. self.traverse_single(fkc)
  869. for seq in seq_coll:
  870. self.traverse_single(seq, drop_ok=seq.column is None)
  871. def _can_drop_table(self, table):
  872. self.dialect.validate_identifier(table.name)
  873. effective_schema = self.connection.schema_for_object(table)
  874. if effective_schema:
  875. self.dialect.validate_identifier(effective_schema)
  876. return not self.checkfirst or self.dialect.has_table(
  877. self.connection, table.name, schema=effective_schema
  878. )
  879. def _can_drop_index(self, index):
  880. effective_schema = self.connection.schema_for_object(index.table)
  881. if effective_schema:
  882. self.dialect.validate_identifier(effective_schema)
  883. return not self.checkfirst or self.dialect.has_index(
  884. self.connection,
  885. index.table.name,
  886. index.name,
  887. schema=effective_schema,
  888. )
  889. def _can_drop_sequence(self, sequence):
  890. effective_schema = self.connection.schema_for_object(sequence)
  891. return self.dialect.supports_sequences and (
  892. (not self.dialect.sequences_optional or not sequence.optional)
  893. and (
  894. not self.checkfirst
  895. or self.dialect.has_sequence(
  896. self.connection, sequence.name, schema=effective_schema
  897. )
  898. )
  899. )
  900. def visit_index(self, index, drop_ok=False):
  901. if not drop_ok and not self._can_drop_index(index):
  902. return
  903. with self.with_ddl_events(index):
  904. DropIndex(index)(index, self.connection)
  905. def visit_table(
  906. self,
  907. table,
  908. drop_ok=False,
  909. _is_metadata_operation=False,
  910. _ignore_sequences=(),
  911. ):
  912. if not drop_ok and not self._can_drop_table(table):
  913. return
  914. with self.with_ddl_events(
  915. table,
  916. checkfirst=self.checkfirst,
  917. _is_metadata_operation=_is_metadata_operation,
  918. ):
  919. DropTable(table)._invoke_with(self.connection)
  920. # traverse client side defaults which may refer to server-side
  921. # sequences. noting that some of these client side defaults may
  922. # also be set up as server side defaults
  923. # (see https://docs.sqlalchemy.org/en/
  924. # latest/core/defaults.html
  925. # #associating-a-sequence-as-the-server-side-
  926. # default), so have to be dropped after the table is dropped.
  927. for column in table.columns:
  928. if (
  929. column.default is not None
  930. and column.default not in _ignore_sequences
  931. ):
  932. self.traverse_single(column.default)
  933. def visit_foreign_key_constraint(self, constraint):
  934. if not self.dialect.supports_alter:
  935. return
  936. with self.with_ddl_events(constraint):
  937. DropConstraint(constraint)._invoke_with(self.connection)
  938. def visit_sequence(self, sequence, drop_ok=False):
  939. if not drop_ok and not self._can_drop_sequence(sequence):
  940. return
  941. with self.with_ddl_events(sequence):
  942. DropSequence(sequence)._invoke_with(self.connection)
  943. def sort_tables(
  944. tables: Iterable[TableClause],
  945. skip_fn: Optional[Callable[[ForeignKeyConstraint], bool]] = None,
  946. extra_dependencies: Optional[
  947. typing_Sequence[Tuple[TableClause, TableClause]]
  948. ] = None,
  949. ) -> List[Table]:
  950. """Sort a collection of :class:`_schema.Table` objects based on
  951. dependency.
  952. This is a dependency-ordered sort which will emit :class:`_schema.Table`
  953. objects such that they will follow their dependent :class:`_schema.Table`
  954. objects.
  955. Tables are dependent on another based on the presence of
  956. :class:`_schema.ForeignKeyConstraint`
  957. objects as well as explicit dependencies
  958. added by :meth:`_schema.Table.add_is_dependent_on`.
  959. .. warning::
  960. The :func:`._schema.sort_tables` function cannot by itself
  961. accommodate automatic resolution of dependency cycles between
  962. tables, which are usually caused by mutually dependent foreign key
  963. constraints. When these cycles are detected, the foreign keys
  964. of these tables are omitted from consideration in the sort.
  965. A warning is emitted when this condition occurs, which will be an
  966. exception raise in a future release. Tables which are not part
  967. of the cycle will still be returned in dependency order.
  968. To resolve these cycles, the
  969. :paramref:`_schema.ForeignKeyConstraint.use_alter` parameter may be
  970. applied to those constraints which create a cycle. Alternatively,
  971. the :func:`_schema.sort_tables_and_constraints` function will
  972. automatically return foreign key constraints in a separate
  973. collection when cycles are detected so that they may be applied
  974. to a schema separately.
  975. .. versionchanged:: 1.3.17 - a warning is emitted when
  976. :func:`_schema.sort_tables` cannot perform a proper sort due to
  977. cyclical dependencies. This will be an exception in a future
  978. release. Additionally, the sort will continue to return
  979. other tables not involved in the cycle in dependency order
  980. which was not the case previously.
  981. :param tables: a sequence of :class:`_schema.Table` objects.
  982. :param skip_fn: optional callable which will be passed a
  983. :class:`_schema.ForeignKeyConstraint` object; if it returns True, this
  984. constraint will not be considered as a dependency. Note this is
  985. **different** from the same parameter in
  986. :func:`.sort_tables_and_constraints`, which is
  987. instead passed the owning :class:`_schema.ForeignKeyConstraint` object.
  988. :param extra_dependencies: a sequence of 2-tuples of tables which will
  989. also be considered as dependent on each other.
  990. .. seealso::
  991. :func:`.sort_tables_and_constraints`
  992. :attr:`_schema.MetaData.sorted_tables` - uses this function to sort
  993. """
  994. if skip_fn is not None:
  995. fixed_skip_fn = skip_fn
  996. def _skip_fn(fkc):
  997. for fk in fkc.elements:
  998. if fixed_skip_fn(fk):
  999. return True
  1000. else:
  1001. return None
  1002. else:
  1003. _skip_fn = None # type: ignore
  1004. return [
  1005. t
  1006. for (t, fkcs) in sort_tables_and_constraints(
  1007. tables,
  1008. filter_fn=_skip_fn,
  1009. extra_dependencies=extra_dependencies,
  1010. _warn_for_cycles=True,
  1011. )
  1012. if t is not None
  1013. ]
  1014. def sort_tables_and_constraints(
  1015. tables, filter_fn=None, extra_dependencies=None, _warn_for_cycles=False
  1016. ):
  1017. """Sort a collection of :class:`_schema.Table` /
  1018. :class:`_schema.ForeignKeyConstraint`
  1019. objects.
  1020. This is a dependency-ordered sort which will emit tuples of
  1021. ``(Table, [ForeignKeyConstraint, ...])`` such that each
  1022. :class:`_schema.Table` follows its dependent :class:`_schema.Table`
  1023. objects.
  1024. Remaining :class:`_schema.ForeignKeyConstraint`
  1025. objects that are separate due to
  1026. dependency rules not satisfied by the sort are emitted afterwards
  1027. as ``(None, [ForeignKeyConstraint ...])``.
  1028. Tables are dependent on another based on the presence of
  1029. :class:`_schema.ForeignKeyConstraint` objects, explicit dependencies
  1030. added by :meth:`_schema.Table.add_is_dependent_on`,
  1031. as well as dependencies
  1032. stated here using the :paramref:`~.sort_tables_and_constraints.skip_fn`
  1033. and/or :paramref:`~.sort_tables_and_constraints.extra_dependencies`
  1034. parameters.
  1035. :param tables: a sequence of :class:`_schema.Table` objects.
  1036. :param filter_fn: optional callable which will be passed a
  1037. :class:`_schema.ForeignKeyConstraint` object,
  1038. and returns a value based on
  1039. whether this constraint should definitely be included or excluded as
  1040. an inline constraint, or neither. If it returns False, the constraint
  1041. will definitely be included as a dependency that cannot be subject
  1042. to ALTER; if True, it will **only** be included as an ALTER result at
  1043. the end. Returning None means the constraint is included in the
  1044. table-based result unless it is detected as part of a dependency cycle.
  1045. :param extra_dependencies: a sequence of 2-tuples of tables which will
  1046. also be considered as dependent on each other.
  1047. .. seealso::
  1048. :func:`.sort_tables`
  1049. """
  1050. fixed_dependencies = set()
  1051. mutable_dependencies = set()
  1052. if extra_dependencies is not None:
  1053. fixed_dependencies.update(extra_dependencies)
  1054. remaining_fkcs = set()
  1055. for table in tables:
  1056. for fkc in table.foreign_key_constraints:
  1057. if fkc.use_alter is True:
  1058. remaining_fkcs.add(fkc)
  1059. continue
  1060. if filter_fn:
  1061. filtered = filter_fn(fkc)
  1062. if filtered is True:
  1063. remaining_fkcs.add(fkc)
  1064. continue
  1065. dependent_on = fkc.referred_table
  1066. if dependent_on is not table:
  1067. mutable_dependencies.add((dependent_on, table))
  1068. fixed_dependencies.update(
  1069. (parent, table) for parent in table._extra_dependencies
  1070. )
  1071. try:
  1072. candidate_sort = list(
  1073. topological.sort(
  1074. fixed_dependencies.union(mutable_dependencies),
  1075. tables,
  1076. )
  1077. )
  1078. except exc.CircularDependencyError as err:
  1079. if _warn_for_cycles:
  1080. util.warn(
  1081. "Cannot correctly sort tables; there are unresolvable cycles "
  1082. 'between tables "%s", which is usually caused by mutually '
  1083. "dependent foreign key constraints. Foreign key constraints "
  1084. "involving these tables will not be considered; this warning "
  1085. "may raise an error in a future release."
  1086. % (", ".join(sorted(t.fullname for t in err.cycles)),)
  1087. )
  1088. for edge in err.edges:
  1089. if edge in mutable_dependencies:
  1090. table = edge[1]
  1091. if table not in err.cycles:
  1092. continue
  1093. can_remove = [
  1094. fkc
  1095. for fkc in table.foreign_key_constraints
  1096. if filter_fn is None or filter_fn(fkc) is not False
  1097. ]
  1098. remaining_fkcs.update(can_remove)
  1099. for fkc in can_remove:
  1100. dependent_on = fkc.referred_table
  1101. if dependent_on is not table:
  1102. mutable_dependencies.discard((dependent_on, table))
  1103. candidate_sort = list(
  1104. topological.sort(
  1105. fixed_dependencies.union(mutable_dependencies),
  1106. tables,
  1107. )
  1108. )
  1109. return [
  1110. (table, table.foreign_key_constraints.difference(remaining_fkcs))
  1111. for table in candidate_sort
  1112. ] + [(None, list(remaining_fkcs))]