class documentation

class ForeignKey(DialectKWArgs, SchemaItem):

View In Hierarchy

Defines a dependency between two columns.

ForeignKey is specified as an argument to a _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 _schema.ForeignKeyConstraint object. This object will be generated automatically when a ForeignKey is associated with a _schema.Column which in turn is associated with a _schema.Table. Conversely, when _schema.ForeignKeyConstraint is applied to a _schema.Table, ForeignKey markers are automatically generated to be present on each associated _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 _schema.ForeignKeyConstraint object must be used, and applied to the _schema.Table. The associated ForeignKey objects are created automatically.

The ForeignKey objects associated with an individual _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 Produce a copy of this _schema.ForeignKey object.
Method ​_get​_colspec Return a string based 'column specification' for this _schema.ForeignKey.
Method ​_link​_to​_col​_by​_colstring Undocumented
Method ​_remove​_from​_metadata Undocumented
Method ​_resolve​_col​_tokens 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
Method copy Undocumented
Method get​_referent Return the _schema.Column in the given _schema.Table referenced by this _schema.ForeignKey.
Method references Return True if the given _schema.Table is referenced by this _schema.ForeignKey.
Class Variable __visit​_name__ Undocumented
Class Variable target​_fullname Undocumented
Instance Variable ​_colspec Undocumented
Instance Variable ​_table​_column Undocumented
Instance Variable ​_unvalidated​_dialect​_kw 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 .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​_tokens parse a string-based _colspec into its component parts.
Property ​_referred​_schema Undocumented
Property column Return the target _schema.Column referenced by this _schema.ForeignKey.

Inherited from DialectKWArgs:

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:

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 SchemaItem):

Method ​_set​_parent​_with​_dispatch Undocumented

Inherited from Traversible (via SchemaItem):

Method get​_children Return immediate child .visitors.Traversible elements of this .visitors.Traversible.
Method __class​_getitem__ Undocumented
def __init__(self, column, _constraint=None, use_alter=False, name=None, onupdate=None, ondelete=None, deferrable=None, initially=None, link_to_name=False, match=None, info=None, **dialect_kw):

Construct a column-level FOREIGN KEY.

The _schema.ForeignKey object when constructed generates a _schema.ForeignKeyConstraint which is associated with the parent _schema.Table object's collection of constraints.

Parameters
columnA single target column for the key relationship. A _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.
​_constraintUndocumented
use​_alter

passed to the underlying _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.

nameOptional string. An in-database name for the key if constraint is not provided.
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.
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.
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>. The arguments are ultimately handled by a corresponding _schema.ForeignKeyConstraint. See the documentation regarding an individual dialect at :ref:`dialect_toplevel` for detail on documented arguments.

New in version 0.9.2.
def __repr__(self):
def _copy(self, schema=None, **kw):

Produce a copy of this _schema.ForeignKey object.

The new _schema.ForeignKey will not be bound to any _schema.Column.

This method is usually used by the internal copy procedures of _schema.Column, _schema.Table, and _schema.MetaData.

Parameters
schemaThe returned _schema.ForeignKey will reference the original table and column name, qualified by the given string schema name.
**kwUndocumented
def _get_colspec(self, schema=None, table_name=None):

Return a string based 'column specification' for this _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, colname):

Undocumented

def _remove_from_metadata(self, metadata):

Undocumented

def _resolve_col_tokens(self):

Undocumented

def _set_parent(self, column, **kw):
Associate with this SchemaEvent's parent object.
def _set_remote_table(self, table):

Undocumented

def _set_table(self, column, table):

Undocumented

def _set_target_column(self, column):

Undocumented

def _table_key(self):

Undocumented

@util.deprecated('1.4', 'The :meth:`_schema.ForeignKey.copy` method is deprecated and will be removed in a future release.')
def copy(self, schema=None, **kw):

Undocumented

def get_referent(self, table):

Return the _schema.Column in the given _schema.Table referenced by this _schema.ForeignKey.

Returns None if this _schema.ForeignKey does not reference the given _schema.Table.

def references(self, table):
Return True if the given _schema.Table is referenced by this _schema.ForeignKey.
__visit_name__: str =
target_fullname =

Undocumented

_colspec =

Undocumented

_table_column =

Undocumented

_unvalidated_dialect_kw =

Undocumented

constraint =

Undocumented

deferrable =

Undocumented

info =

Info dictionary associated with the object, allowing user-defined data to be associated with this .SchemaItem.

The dictionary is automatically generated when first accessed. It can also be specified in the constructor of some objects, such as _schema.Table and _schema.Column.

initially =

Undocumented

link_to_name =

Undocumented

match =

Undocumented

name =

Undocumented

ondelete =

Undocumented

onupdate =

Undocumented

parent =

Undocumented

use_alter =

Undocumented

@util.memoized_property
_column_tokens =
parse a string-based _colspec into its component parts.
@property
_referred_schema =

Undocumented

Return the target _schema.Column referenced by this _schema.ForeignKey.

If no target column has been established, an exception is raised.

Changed in version 0.9.0: Foreign key target column resolution now occurs as soon as both the ForeignKey object and the remote Column to which it refers are both associated with the same MetaData object.