MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / ForeignKey

Class ForeignKey

lib/sqlalchemy/sql/schema.py:2755–3322  ·  view source on GitHub ↗

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 `

Source from the content-addressed store, hash-verified

2753
2754
2755class ForeignKey(DialectKWArgs, SchemaItem):
2756 """Defines a dependency between two columns.
2757
2758 ``ForeignKey`` is specified as an argument to a :class:`_schema.Column`
2759 object,
2760 e.g.::
2761
2762 t = Table(
2763 "remote_table",
2764 metadata,
2765 Column("remote_id", ForeignKey("main_table.id")),
2766 )
2767
2768 Note that ``ForeignKey`` is only a marker object that defines
2769 a dependency between two columns. The actual constraint
2770 is in all cases represented by the :class:`_schema.ForeignKeyConstraint`
2771 object. This object will be generated automatically when
2772 a ``ForeignKey`` is associated with a :class:`_schema.Column` which
2773 in turn is associated with a :class:`_schema.Table`. Conversely,
2774 when :class:`_schema.ForeignKeyConstraint` is applied to a
2775 :class:`_schema.Table`,
2776 ``ForeignKey`` markers are automatically generated to be
2777 present on each associated :class:`_schema.Column`, which are also
2778 associated with the constraint object.
2779
2780 Note that you cannot define a "composite" foreign key constraint,
2781 that is a constraint between a grouping of multiple parent/child
2782 columns, using ``ForeignKey`` objects. To define this grouping,
2783 the :class:`_schema.ForeignKeyConstraint` object must be used, and applied
2784 to the :class:`_schema.Table`. The associated ``ForeignKey`` objects
2785 are created automatically.
2786
2787 The ``ForeignKey`` objects associated with an individual
2788 :class:`_schema.Column`
2789 object are available in the `foreign_keys` collection
2790 of that column.
2791
2792 Further examples of foreign key configuration are in
2793 :ref:`metadata_foreignkeys`.
2794
2795 """
2796
2797 __visit_name__ = "foreign_key"
2798
2799 parent: Column[Any]
2800
2801 _table_column: Optional[Column[Any]]
2802
2803 def __init__(
2804 self,
2805 column: _DDLColumnArgument,
2806 _constraint: Optional[ForeignKeyConstraint] = None,
2807 use_alter: bool = False,
2808 name: _ConstraintNameArgument = None,
2809 onupdate: Optional[str] = None,
2810 ondelete: Optional[str] = None,
2811 deferrable: Optional[bool] = None,
2812 initially: Optional[str] = None,

Callers 15

define_tablesMethod · 0.90
define_tablesMethod · 0.90
define_tablesMethod · 0.90
define_tablesMethod · 0.90
define_tablesMethod · 0.90
define_tablesMethod · 0.90
define_tablesMethod · 0.90
define_tablesMethod · 0.90
test_select.pyFile · 0.90

Calls

no outgoing calls

Tested by 15

define_tablesMethod · 0.72
define_tablesMethod · 0.72
define_tablesMethod · 0.72
define_tablesMethod · 0.72
define_tablesMethod · 0.72
define_tablesMethod · 0.72
define_tablesMethod · 0.72
define_tablesMethod · 0.72