class documentation

Defines a dependency between two columns. ``ForeignKey`` is specified as an argument to a :class:`_schema.Column` object, e.g.:: t = Table("remote_table", metadata, Column("remote_id", ForeignKey("main_table.id")) ) Note that ``ForeignKey`` is only a marker object that defines a dependency between two columns. The actual constraint is in all cases represented by the :class:`_schema.ForeignKeyConstraint` object. This object will be generated automatically when a ``ForeignKey`` is associated with a :class:`_schema.Column` which in turn is associated with a :class:`_schema.Table`. Conversely, when :class:`_schema.ForeignKeyConstraint` is applied to a :class:`_schema.Table`, ``ForeignKey`` markers are automatically generated to be present on each associated :class:`_schema.Column`, which are also associated with the constraint object. Note that you cannot define a "composite" foreign key constraint, that is a constraint between a grouping of multiple parent/child columns, using ``ForeignKey`` objects. To define this grouping, the :class:`_schema.ForeignKeyConstraint` object must be used, and applied to the :class:`_schema.Table`. The associated ``ForeignKey`` objects are created automatically. The ``ForeignKey`` objects associated with an individual :class:`_schema.Column` object are available in the `foreign_keys` collection of that column. Further examples of foreign key configuration are in :ref:`metadata_foreignkeys`.

Method __init__ Construct a column-level FOREIGN KEY.
Method __repr__ Undocumented
Method copy Undocumented
Method get_referent Return the :class:`_schema.Column` in the given :class:`_schema.Table` (or any :class:`.FromClause`) referenced by this :class:`_schema.ForeignKey`.
Method references Return True if the given :class:`_schema.Table` is referenced by this :class:`_schema.ForeignKey`.
Class Variable __visit_name__ Undocumented
Class Variable target_fullname Undocumented
Instance Variable comment Undocumented
Instance Variable constraint Undocumented
Instance Variable deferrable Undocumented
Instance Variable info Info dictionary associated with the object, allowing user-defined data to be associated with this :class:`.SchemaItem`.
Instance Variable initially Undocumented
Instance Variable link_to_name Undocumented
Instance Variable match Undocumented
Instance Variable name Undocumented
Instance Variable ondelete Undocumented
Instance Variable onupdate Undocumented
Instance Variable parent Undocumented
Instance Variable use_alter Undocumented
Property column Return the target :class:`_schema.Column` referenced by this :class:`_schema.ForeignKey`.
Method _copy Produce a copy of this :class:`_schema.ForeignKey` object.
Method _get_colspec Return a string based 'column specification' for this :class:`_schema.ForeignKey`.
Method _link_to_col_by_colstring Undocumented
Method _remove_from_metadata Undocumented
Method _resolve_col_tokens Undocumented
Method _resolve_column Undocumented
Method _set_parent Associate with this SchemaEvent's parent object.
Method _set_remote_table Undocumented
Method _set_table Undocumented
Method _set_target_column Undocumented
Method _table_key Undocumented
Instance Variable _colspec Undocumented
Instance Variable _table_column Undocumented
Instance Variable _unresolvable Undocumented
Instance Variable _unvalidated_dialect_kw Undocumented
Property _column_tokens parse a string-based _colspec into its component parts.
Property _referred_schema Undocumented

Inherited from DialectKWArgs:

Class Method argument_for Add a new kind of dialect-specific keyword argument for this class.
Class Variable __slots__ Undocumented
Property dialect_kwargs A collection of keyword arguments specified as dialect-specific options to this construct.
Property dialect_options A collection of keyword arguments specified as dialect-specific options to this construct.
Property kwargs A synonym for :attr:`.DialectKWArgs.dialect_kwargs`.
Method _kw_reg_for_dialect_cls Undocumented
Method _validate_dialect_kwargs Undocumented
Class Variable _dialect_kwargs_traverse_internals Undocumented
Class Variable _kw_registry Undocumented

Inherited from SchemaItem (via DialectKWArgs):

Class Variable create_drop_stringify_dialect Undocumented
Method _init_items Initialize the list of child items for this SchemaItem.
Method _schema_item_copy Undocumented
Class Variable _use_schema_map Undocumented

Inherited from SchemaEventTarget (via DialectKWArgs, SchemaItem):

Class Variable dispatch Undocumented
Method _set_parent_with_dispatch Undocumented

Inherited from Visitable (via DialectKWArgs, SchemaItem, SchemaEventTarget, EventTarget):

Method __class_getitem__ Undocumented
Method __init_subclass__ Undocumented
Class Method _generate_compiler_dispatch Undocumented
Method _compiler_dispatch Undocumented
Class Variable _original_compiler_dispatch Undocumented
def __init__(self, column: _DDLColumnArgument, _constraint: Optional[ForeignKeyConstraint] = None, use_alter: bool = False, name: _ConstraintNameArgument = None, onupdate: Optional[str] = None, ondelete: Optional[str] = None, deferrable: Optional[bool] = None, initially: Optional[str] = None, link_to_name: bool = False, match: Optional[str] = None, info: Optional[_InfoType] = None, comment: Optional[str] = None, _unresolvable: bool = False, **dialect_kw: Any): (source)

Construct a column-level FOREIGN KEY. The :class:`_schema.ForeignKey` object when constructed generates a :class:`_schema.ForeignKeyConstraint` which is associated with the parent :class:`_schema.Table` object's collection of constraints. :param column: A single target column for the key relationship. A :class:`_schema.Column` object or a column name as a string: ``tablename.columnkey`` or ``schema.tablename.columnkey``. ``columnkey`` is the ``key`` which has been assigned to the column (defaults to the column name itself), unless ``link_to_name`` is ``True`` in which case the rendered name of the column is used. :param name: Optional string. An in-database name for the key if `constraint` is not provided. :param onupdate: Optional string. If set, emit ON UPDATE <value> when issuing DDL for this constraint. Typical values include CASCADE, DELETE and RESTRICT. :param ondelete: Optional string. If set, emit ON DELETE <value> when issuing DDL for this constraint. Typical values include CASCADE, DELETE and RESTRICT. :param deferrable: Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when issuing DDL for this constraint. :param initially: Optional string. If set, emit INITIALLY <value> when issuing DDL for this constraint. :param link_to_name: if True, the string name given in ``column`` is the rendered name of the referenced column, not its locally assigned ``key``. :param use_alter: passed to the underlying :class:`_schema.ForeignKeyConstraint` to indicate the constraint should be generated/dropped externally from the CREATE TABLE/ DROP TABLE statement. See :paramref:`_schema.ForeignKeyConstraint.use_alter` for further description. .. seealso:: :paramref:`_schema.ForeignKeyConstraint.use_alter` :ref:`use_alter` :param match: Optional string. If set, emit MATCH <value> when issuing DDL for this constraint. Typical values include SIMPLE, PARTIAL and FULL. :param info: Optional data dictionary which will be populated into the :attr:`.SchemaItem.info` attribute of this object. .. versionadded:: 1.0.0 :param comment: Optional string that will render an SQL comment on foreign key constraint creation. .. versionadded:: 2.0 :param \**dialect_kw: Additional keyword arguments are dialect specific, and passed in the form ``<dialectname>_<argname>``. The arguments are ultimately handled by a corresponding :class:`_schema.ForeignKeyConstraint`. See the documentation regarding an individual dialect at :ref:`dialect_toplevel` for detail on documented arguments. .. versionadded:: 0.9.2

def __repr__(self) -> str: (source)
@util.deprecated('1.4', 'The :meth:`_schema.ForeignKey.copy` method is deprecated and will be removed in a future release.')
def copy(self, *, schema: Optional[str] = None, **kw: Any) -> ForeignKey: (source)

Undocumented

def get_referent(self, table: FromClause) -> Optional[Column[Any]]: (source)

Return the :class:`_schema.Column` in the given :class:`_schema.Table` (or any :class:`.FromClause`) referenced by this :class:`_schema.ForeignKey`. Returns None if this :class:`_schema.ForeignKey` does not reference the given :class:`_schema.Table`.

def references(self, table: Table) -> bool: (source)

Return True if the given :class:`_schema.Table` is referenced by this :class:`_schema.ForeignKey`.

target_fullname = (source)

Undocumented

Undocumented

constraint = (source)

Undocumented

deferrable = (source)

Undocumented

Info dictionary associated with the object, allowing user-defined data to be associated with this :class:`.SchemaItem`. The dictionary is automatically generated when first accessed. It can also be specified in the constructor of some objects, such as :class:`_schema.Table` and :class:`_schema.Column`.

initially = (source)

Undocumented

link_to_name = (source)

Undocumented

Undocumented

Undocumented

ondelete = (source)

Undocumented

onupdate = (source)

Undocumented

Undocumented

use_alter = (source)

Undocumented

@util.ro_memoized_property
column: Column[Any] = (source)

Return the target :class:`_schema.Column` referenced by this :class:`_schema.ForeignKey`. If no target column has been established, an exception is raised.

def _copy(self, *, schema: Optional[str] = None, **kw: Any) -> ForeignKey: (source)

Produce a copy of this :class:`_schema.ForeignKey` object. The new :class:`_schema.ForeignKey` will not be bound to any :class:`_schema.Column`. This method is usually used by the internal copy procedures of :class:`_schema.Column`, :class:`_schema.Table`, and :class:`_schema.MetaData`. :param schema: The returned :class:`_schema.ForeignKey` will reference the original table and column name, qualified by the given string schema name.

def _get_colspec(self, schema: Optional[Union[str, Literal[SchemaConst.RETAIN_SCHEMA, SchemaConst.BLANK_SCHEMA]]] = None, table_name: Optional[str] = None, _is_copy: bool = False) -> str: (source)

Return a string based 'column specification' for this :class:`_schema.ForeignKey`. This is usually the equivalent of the string-based "tablename.colname" argument first passed to the object's constructor.

def _link_to_col_by_colstring(self, parenttable: Table, table: Table, colname: Optional[str]) -> Column[Any]: (source)

Undocumented

def _remove_from_metadata(self, metadata: MetaData): (source)

Undocumented

def _resolve_col_tokens(self) -> Tuple[Table, str, Optional[str]]: (source)

Undocumented

@overload
def _resolve_column(self, *, raiseerr: Literal[True] = ...) -> Column[Any]:
@overload
def _resolve_column(self, *, raiseerr: bool = ...) -> Optional[Column[Any]]:
(source)

Undocumented

def _set_parent(self, parent: SchemaEventTarget, **kw: Any): (source)

Associate with this SchemaEvent's parent object.

def _set_remote_table(self, table: Table): (source)

Undocumented

def _set_table(self, column: Column[Any], table: Table): (source)

Undocumented

def _set_target_column(self, column: Column[Any]): (source)

Undocumented

def _table_key(self) -> Any: (source)

Undocumented

_colspec = (source)

Undocumented

_table_column = (source)

Undocumented

_unresolvable = (source)

Undocumented

_unvalidated_dialect_kw = (source)

Undocumented

@util.memoized_property
_column_tokens: Tuple[Optional[str], str, Optional[str]] = (source)

parse a string-based _colspec into its component parts.

@property
_referred_schema: Optional[str] = (source)

Undocumented