class documentation

class PrimaryKeyConstraint(ColumnCollectionConstraint):

View In Hierarchy

A table-level PRIMARY KEY constraint.

The .PrimaryKeyConstraint object is present automatically on any _schema.Table object; it is assigned a set of _schema.Column objects corresponding to those marked with the :paramref:`_schema.Column.primary_key` flag:

>>> my_table = Table('mytable', metadata,
...                 Column('id', Integer, primary_key=True),
...                 Column('version_id', Integer, primary_key=True),
...                 Column('data', String(50))
...     )
>>> my_table.primary_key
PrimaryKeyConstraint(
    Column('id', Integer(), table=<mytable>,
           primary_key=True, nullable=False),
    Column('version_id', Integer(), table=<mytable>,
           primary_key=True, nullable=False)
)

The primary key of a _schema.Table can also be specified by using a .PrimaryKeyConstraint object explicitly; in this mode of usage, the "name" of the constraint can also be specified, as well as other options which may be recognized by dialects:

my_table = Table('mytable', metadata,
            Column('id', Integer),
            Column('version_id', Integer),
            Column('data', String(50)),
            PrimaryKeyConstraint('id', 'version_id',
                                 name='mytable_pk')
        )

The two styles of column-specification should generally not be mixed. An warning is emitted if the columns present in the .PrimaryKeyConstraint don't match the columns that were marked as primary_key=True, if both are present; in this case, the columns are taken strictly from the .PrimaryKeyConstraint declaration, and those columns otherwise marked as primary_key=True are ignored. This behavior is intended to be backwards compatible with previous behavior.

Changed in version 0.9.2: Using a mixture of columns within a .PrimaryKeyConstraint in addition to columns marked as primary_key=True now emits a warning if the lists don't match. The ultimate behavior of ignoring those columns marked with the flag only is currently maintained for backwards compatibility; this warning may raise an exception in a future release.

For the use case where specific options are to be specified on the .PrimaryKeyConstraint, but the usual style of using primary_key=True flags is still desirable, an empty .PrimaryKeyConstraint may be specified, which will take on the primary key column collection from the _schema.Table based on the flags:

my_table = Table('mytable', metadata,
            Column('id', Integer, primary_key=True),
            Column('version_id', Integer, primary_key=True),
            Column('data', String(50)),
            PrimaryKeyConstraint(name='mytable_pk',
                                 mssql_clustered=True)
        )
New in version 0.9.2: an empty .PrimaryKeyConstraint may now be specified for the purposes of establishing keyword arguments with the constraint, independently of the specification of "primary key" columns within the _schema.Table itself; columns marked as primary_key=True will be gathered into the empty constraint's column collection.
Method __init__
Method ​_reload repopulate this .PrimaryKeyConstraint given a set of columns.
Method ​_replace Undocumented
Method ​_set​_parent Associate with this SchemaEvent's parent object.
Class Variable __visit​_name__ Undocumented
Instance Variable ​_implicit​_generated Undocumented
Property ​_autoincrement​_column Undocumented
Property columns​_autoinc​_first Undocumented

Inherited from ColumnCollectionConstraint:

Method __contains__ Undocumented
Method __iter__ Undocumented
Method __len__ Undocumented
Method ​_copy Undocumented
Method contains​_column Return True if this constraint contains the given column.
Method copy Undocumented
Class Variable columns A _expression.ColumnCollection representing the set of columns for this constraint.

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, **kw):
Parameters
*columnsA sequence of column names or Column objects.
**kwother keyword arguments including dialect-specific arguments are propagated to the .Constraint superclass.
nameOptional, the in-database name of this constraint.
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.
def _reload(self, columns):

repopulate this .PrimaryKeyConstraint given a set of columns.

Existing columns in the table that are marked as primary_key=True are maintained.

Also fires a new event.

This is basically like putting a whole new .PrimaryKeyConstraint object on the parent _schema.Table object without actually replacing the object.

The ordering of the given list of columns is also maintained; these columns will be appended to the list of columns after any which are already present.

def _replace(self, col):

Undocumented

def _set_parent(self, table, **kw):
Associate with this SchemaEvent's parent object.
__visit_name__: str =
_implicit_generated =

Undocumented

@util.memoized_property
_autoincrement_column =

Undocumented

@property
columns_autoinc_first =

Undocumented