MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / PrimaryKeyConstraint

Class PrimaryKeyConstraint

lib/sqlalchemy/sql/schema.py:5279–5521  ·  view source on GitHub ↗

A table-level PRIMARY KEY constraint. The :class:`.PrimaryKeyConstraint` object is present automatically on any :class:`_schema.Table` object; it is assigned a set of :class:`_schema.Column` objects corresponding to those marked with the :paramref:`_schema.Column.primary_key` flag::

Source from the content-addressed store, hash-verified

5277
5278
5279class PrimaryKeyConstraint(ColumnCollectionConstraint):
5280 """A table-level PRIMARY KEY constraint.
5281
5282 The :class:`.PrimaryKeyConstraint` object is present automatically
5283 on any :class:`_schema.Table` object; it is assigned a set of
5284 :class:`_schema.Column` objects corresponding to those marked with
5285 the :paramref:`_schema.Column.primary_key` flag::
5286
5287 >>> my_table = Table(
5288 ... "mytable",
5289 ... metadata,
5290 ... Column("id", Integer, primary_key=True),
5291 ... Column("version_id", Integer, primary_key=True),
5292 ... Column("data", String(50)),
5293 ... )
5294 >>> my_table.primary_key
5295 PrimaryKeyConstraint(
5296 Column('id', Integer(), table=<mytable>,
5297 primary_key=True, nullable=False),
5298 Column('version_id', Integer(), table=<mytable>,
5299 primary_key=True, nullable=False)
5300 )
5301
5302 The primary key of a :class:`_schema.Table` can also be specified by using
5303 a :class:`.PrimaryKeyConstraint` object explicitly; in this mode of usage,
5304 the "name" of the constraint can also be specified, as well as other
5305 options which may be recognized by dialects::
5306
5307 my_table = Table(
5308 "mytable",
5309 metadata,
5310 Column("id", Integer),
5311 Column("version_id", Integer),
5312 Column("data", String(50)),
5313 PrimaryKeyConstraint("id", "version_id", name="mytable_pk"),
5314 )
5315
5316 The two styles of column-specification should generally not be mixed.
5317 An warning is emitted if the columns present in the
5318 :class:`.PrimaryKeyConstraint`
5319 don&#x27;t match the columns that were marked as ``primary_key=True``, if both
5320 are present; in this case, the columns are taken strictly from the
5321 :class:`.PrimaryKeyConstraint` declaration, and those columns otherwise
5322 marked as ``primary_key=True`` are ignored. This behavior is intended to
5323 be backwards compatible with previous behavior.
5324
5325 For the use case where specific options are to be specified on the
5326 :class:`.PrimaryKeyConstraint`, but the usual style of using
5327 ``primary_key=True`` flags is still desirable, an empty
5328 :class:`.PrimaryKeyConstraint` may be specified, which will take on the
5329 primary key column collection from the :class:`_schema.Table` based on the
5330 flags::
5331
5332 my_table = Table(
5333 "mytable",
5334 metadata,
5335 Column("id", Integer, primary_key=True),
5336 Column("version_id", Integer, primary_key=True),

Calls

no outgoing calls