class documentation

class ForeignKeyConstraint(ColumnCollectionConstraint):

View In Hierarchy

A table-level FOREIGN KEY constraint.

Defines a single column or composite FOREIGN KEY ... REFERENCES constraint. For a no-frills, single column foreign key, adding a _schema.ForeignKey to the definition of a _schema.Column is a shorthand equivalent for an unnamed, single column _schema.ForeignKeyConstraint.

Examples of foreign key configuration are in :ref:`metadata_foreignkeys`.

Method __init__ Construct a composite-capable FOREIGN KEY.
Method ​_append​_element Undocumented
Method ​_copy Undocumented
Method ​_set​_parent Associate with this SchemaEvent's parent object.
Method ​_validate​_dest​_table Undocumented
Method copy Undocumented
Class Variable __visit​_name__ Undocumented
Class Variable columns A _expression.ColumnCollection representing the set of columns for this constraint.
Instance Variable elements A sequence of _schema.ForeignKey objects.
Instance Variable link​_to​_name Undocumented
Instance Variable match Undocumented
Instance Variable ondelete Undocumented
Instance Variable onupdate Undocumented
Instance Variable use​_alter Undocumented
Property ​_col​_description Undocumented
Property ​_elements Undocumented
Property ​_referred​_schema Undocumented
Property column​_keys Return a list of string keys representing the local columns in this _schema.ForeignKeyConstraint.
Property referred​_table The _schema.Table object to which this _schema.ForeignKeyConstraint references.

Inherited from ColumnCollectionConstraint:

Method __contains__ Undocumented
Method __iter__ Undocumented
Method __len__ Undocumented
Method contains​_column Return True if this constraint contains the given column.

Inherited from ColumnCollectionMixin (via ColumnCollectionConstraint):

Method ​_check​_attach Undocumented
Method ​_col​_expressions Undocumented
Class Variable ​_allow​_multiple​_tables Undocumented
Instance Variable ​_cols​_wo​_table Undocumented
Instance Variable ​_column​_flag Undocumented
Instance Variable ​_pending​_colargs Undocumented

Inherited from Constraint (via ColumnCollectionConstraint):

Instance Variable ​_create​_rule Undocumented
Instance Variable ​_type​_bound Undocumented
Instance Variable deferrable Undocumented
Instance Variable info Info dictionary associated with the object, allowing user-defined data to be associated with this .SchemaItem.
Instance Variable initially Undocumented
Instance Variable name Undocumented
Instance Variable parent Undocumented
Property table Undocumented

Inherited from DialectKWArgs (via ColumnCollectionConstraint, Constraint):

Class Method argument​_for Add a new kind of dialect-specific keyword argument for this class.
Method ​_kw​_reg​_for​_dialect​_cls Undocumented
Method ​_validate​_dialect​_kwargs Undocumented
Class Variable ​_dialect​_kwargs​_traverse​_internals Undocumented
Class Variable ​_kw​_registry 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 .DialectKWArgs.dialect_kwargs.

Inherited from SchemaItem (via ColumnCollectionConstraint, Constraint):

Method __repr__ Undocumented
Method ​_init​_items Initialize the list of child items for this SchemaItem.
Method ​_schema​_item​_copy Undocumented
Class Variable ​_use​_schema​_map Undocumented
Class Variable create​_drop​_stringify​_dialect Undocumented

Inherited from SchemaEventTarget (via ColumnCollectionConstraint, Constraint, SchemaItem):

Method ​_set​_parent​_with​_dispatch Undocumented

Inherited from Traversible (via ColumnCollectionConstraint, Constraint, SchemaItem):

Method get​_children Return immediate child .visitors.Traversible elements of this .visitors.Traversible.
Method __class​_getitem__ Undocumented
def __init__(self, columns, refcolumns, name=None, onupdate=None, ondelete=None, deferrable=None, initially=None, use_alter=False, link_to_name=False, match=None, table=None, info=None, **dialect_kw):
Construct a composite-capable FOREIGN KEY.
Parameters
columnsA sequence of local column names. The named columns must be defined and present in the parent Table. The names should match the key given to each column (defaults to the name) unless link_to_name is True.
refcolumnsA sequence of foreign column names or Column objects. The columns must all be located within the same Table.
nameOptional, the in-database name of the key.
onupdateOptional string. If set, emit ON UPDATE <value> when issuing DDL for this constraint. Typical values include CASCADE, DELETE and RESTRICT.
ondeleteOptional string. If set, emit ON DELETE <value> when issuing DDL for this constraint. Typical values include CASCADE, DELETE and RESTRICT.
deferrableOptional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when issuing DDL for this constraint.
initiallyOptional string. If set, emit INITIALLY <value> when issuing DDL for this constraint.
use​_alter

If True, do not emit the DDL for this constraint as part of the CREATE TABLE definition. Instead, generate it via an ALTER TABLE statement issued after the full collection of tables have been created, and drop it via an ALTER TABLE statement before the full collection of tables are dropped.

The use of :paramref:`_schema.ForeignKeyConstraint.use_alter` is particularly geared towards the case where two or more tables are established within a mutually-dependent foreign key constraint relationship; however, the _schema.MetaData.create_all and _schema.MetaData.drop_all methods will perform this resolution automatically, so the flag is normally not needed.

Changed in version 1.0.0: Automatic resolution of foreign key cycles has been added, removing the need to use the :paramref:`_schema.ForeignKeyConstraint.use_alter` in typical use cases.

See Also

:ref:`use_alter`

link​_to​_nameif True, the string name given in column is the rendered name of the referenced column, not its locally assigned key.
matchOptional string. If set, emit MATCH <value> when issuing DDL for this constraint. Typical values include SIMPLE, PARTIAL and FULL.
tableUndocumented
info

Optional data dictionary which will be populated into the .SchemaItem.info attribute of this object.

New in version 1.0.0.
**dialect​_kw

Additional keyword arguments are dialect specific, and passed in the form <dialectname>_<argname>. See the documentation regarding an individual dialect at :ref:`dialect_toplevel` for detail on documented arguments.

New in version 0.9.2.
def _append_element(self, column, fk):

Undocumented

def _copy(self, schema=None, target_table=None, **kw):
def _set_parent(self, table, **kw):
Associate with this SchemaEvent's parent object.
def _validate_dest_table(self, table):

Undocumented

@util.deprecated('1.4', 'The :meth:`_schema.ForeignKeyConstraint.copy` method is deprecated and will be removed in a future release.')
def copy(self, schema=None, target_table=None, **kw):
__visit_name__: str =
columns =
A _expression.ColumnCollection representing the set of columns for this constraint.
elements =

A sequence of _schema.ForeignKey objects.

Each _schema.ForeignKey represents a single referring column/referred column pair.

This collection is intended to be read-only.

link_to_name =

Undocumented

match =

Undocumented

ondelete =

Undocumented

onupdate =

Undocumented

use_alter =

Undocumented

@property
_col_description =

Undocumented

@property
_elements =

Undocumented

@property
_referred_schema =

Undocumented

@property
column_keys =

Return a list of string keys representing the local columns in this _schema.ForeignKeyConstraint.

This list is either the original string arguments sent to the constructor of the _schema.ForeignKeyConstraint, or if the constraint has been initialized with _schema.Column objects, is the string .key of each element.

New in version 1.0.0.
@property
referred_table =

The _schema.Table object to which this _schema.ForeignKeyConstraint references.

This is a dynamically calculated attribute which may not be available if the constraint and/or parent table is not yet associated with a metadata collection that contains the referred table.

New in version 1.0.0.