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 `
| 3030 | |
| 3031 | |
| 3032 | class ForeignKey(DialectKWArgs, SchemaItem): |
| 3033 | """Defines a dependency between two columns. |
| 3034 | |
| 3035 | ``ForeignKey`` is specified as an argument to a :class:`_schema.Column` |
| 3036 | object, |
| 3037 | e.g.:: |
| 3038 | |
| 3039 | t = Table( |
| 3040 | "remote_table", |
| 3041 | metadata, |
| 3042 | Column("remote_id", ForeignKey("main_table.id")), |
| 3043 | ) |
| 3044 | |
| 3045 | Note that ``ForeignKey`` is only a marker object that defines |
| 3046 | a dependency between two columns. The actual constraint |
| 3047 | is in all cases represented by the :class:`_schema.ForeignKeyConstraint` |
| 3048 | object. This object will be generated automatically when |
| 3049 | a ``ForeignKey`` is associated with a :class:`_schema.Column` which |
| 3050 | in turn is associated with a :class:`_schema.Table`. Conversely, |
| 3051 | when :class:`_schema.ForeignKeyConstraint` is applied to a |
| 3052 | :class:`_schema.Table`, |
| 3053 | ``ForeignKey`` markers are automatically generated to be |
| 3054 | present on each associated :class:`_schema.Column`, which are also |
| 3055 | associated with the constraint object. |
| 3056 | |
| 3057 | Note that you cannot define a "composite" foreign key constraint, |
| 3058 | that is a constraint between a grouping of multiple parent/child |
| 3059 | columns, using ``ForeignKey`` objects. To define this grouping, |
| 3060 | the :class:`_schema.ForeignKeyConstraint` object must be used, and applied |
| 3061 | to the :class:`_schema.Table`. The associated ``ForeignKey`` objects |
| 3062 | are created automatically. |
| 3063 | |
| 3064 | The ``ForeignKey`` objects associated with an individual |
| 3065 | :class:`_schema.Column` |
| 3066 | object are available in the `foreign_keys` collection |
| 3067 | of that column. |
| 3068 | |
| 3069 | Further examples of foreign key configuration are in |
| 3070 | :ref:`metadata_foreignkeys`. |
| 3071 | |
| 3072 | """ |
| 3073 | |
| 3074 | __visit_name__ = "foreign_key" |
| 3075 | |
| 3076 | parent: Column[Any] |
| 3077 | |
| 3078 | _table_column: Optional[Column[Any]] |
| 3079 | |
| 3080 | _colspec: Union[str, Column[Any]] |
| 3081 | |
| 3082 | def __init__( |
| 3083 | self, |
| 3084 | column: _DDLColumnReferenceArgument, |
| 3085 | _constraint: Optional[ForeignKeyConstraint] = None, |
| 3086 | use_alter: bool = False, |
| 3087 | name: _ConstraintNameArgument = None, |
| 3088 | onupdate: Optional[str] = None, |
| 3089 | ondelete: Optional[str] = None, |
no outgoing calls