@@ -2117,17 +2117,17 @@ def add_column(
21172117 The :meth:`.Operations.add_column` method typically corresponds
21182118 to the SQL command "ALTER TABLE... ADD COLUMN". Within the scope
21192119 of this command, the column's name, datatype, nullability,
2120- and optional server-generated defaults may be indicated.
2120+ and optional server-generated defaults may be indicated. Options
2121+ also exist for control of single-column primary key and foreign key
2122+ constraints to be generated.
21212123
21222124 .. note::
21232125
21242126 Not all contraint types may be indicated with this directive.
2125- PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
2127+ NOT NULL, FOREIGN KEY, and CHECK are honored, PRIMARY KEY
2128+ is conditionally honored, UNIQUE
21262129 is currently not.
21272130
2128- .. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
2129- emitted within :meth:`.Operations.add_column`.
2130-
21312131 As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
21322132 parameters are **ignored**:
21332133
@@ -2136,6 +2136,39 @@ def add_column(
21362136 * :paramref:`~sqlalchemy.schema.Column.index` - use the
21372137 :meth:`.Operations.create_index` method
21382138
2139+ **PRIMARY KEY support**
2140+
2141+ The provided :class:`~sqlalchemy.schema.Column` object may include a
2142+ ``primary_key=True`` directive, indicating the column intends to be
2143+ part of a primary key constraint. However by default, the inline
2144+ "PRIMARY KEY" directive is not emitted, and it's assumed that a
2145+ separate :meth:`.Operations.create_primary_key` directive will be used
2146+ to create this constraint, which may potentially include other columns
2147+ as well as have an explicit name. To instead render an inline
2148+ "PRIMARY KEY" directive, the
2149+ :paramref:`.AddColumnOp.inline_primary_key` parameter may be indicated
2150+ at the same time as the ``primary_key`` parameter (both are needed)::
2151+
2152+ from alembic import op
2153+ from sqlalchemy import Column, INTEGER
2154+
2155+ op.add_column(
2156+ "organization",
2157+ Column("id", INTEGER, primary_key=True),
2158+ inline_primary_key=True
2159+ )
2160+
2161+ The ``primary_key=True`` parameter on
2162+ :class:`~sqlalchemy.schema.Column` also indicates behaviors such as
2163+ using the ``SERIAL`` datatype with the PostgreSQL database, which is
2164+ why two separate, independent parameters are provided to support all
2165+ combinations.
2166+
2167+ .. versionadded:: 1.18.4 Added
2168+ :paramref:`.AddColumnOp.inline_primary_key`
2169+ to control use of the ``PRIMARY KEY`` inline directive.
2170+
2171+ **FOREIGN KEY support**
21392172
21402173 The provided :class:`~sqlalchemy.schema.Column` object may include a
21412174 :class:`~sqlalchemy.schema.ForeignKey` constraint directive,
@@ -2165,6 +2198,8 @@ def add_column(
21652198 inline_references=True,
21662199 )
21672200
2201+ **Indicating server side defaults**
2202+
21682203 The column argument passed to :meth:`.Operations.add_column` is a
21692204 :class:`~sqlalchemy.schema.Column` construct, used in the same way it's
21702205 used in SQLAlchemy. In particular, values or functions to be indicated
@@ -2188,23 +2223,25 @@ def add_column(
21882223 quoting of the schema outside of the default behavior, use
21892224 the SQLAlchemy construct
21902225 :class:`~sqlalchemy.sql.elements.quoted_name`.
2191- :param if_not_exists: If True, adds IF NOT EXISTS operator
2226+ :param if_not_exists: If True, adds `` IF NOT EXISTS`` operator
21922227 when creating the new column for compatible dialects
21932228
21942229 .. versionadded:: 1.16.0
21952230
2196- :param inline_references: If True, renders FOREIGN KEY constraints
2197- inline within the ADD COLUMN directive using REFERENCES syntax,
2198- rather than as a separate ALTER TABLE ADD CONSTRAINT statement.
2199- This is supported by PostgreSQL, Oracle, MySQL 5.7+, and
2231+ :param inline_references: If True, renders `` FOREIGN KEY`` constraints
2232+ inline within the `` ADD COLUMN`` directive using `` REFERENCES``
2233+ syntax, rather than as a separate `` ALTER TABLE ADD CONSTRAINT``
2234+ statement. This is supported by PostgreSQL, Oracle, MySQL 5.7+, and
22002235 MariaDB 10.5+.
22012236
22022237 .. versionadded:: 1.18.2
22032238
2204- :param inline_primary_key: If True, renders the PRIMARY KEY constraint
2205- inline within the ADD COLUMN directive, rather than rendering it
2206- separately. This is a purely syntactic option and should only be
2207- used for single-column primary keys.
2239+ :param inline_primary_key: If True, renders the ``PRIMARY KEY`` phrase
2240+ inline within the ``ADD COLUMN`` directive. When not present or
2241+ False, ``PRIMARY KEY`` is not emitted; it is assumed that the
2242+ migration script will include an additional
2243+ :meth:`.Operations.create_primary_key` directive to create a full
2244+ primary key constraint.
22082245
22092246 .. versionadded:: 1.18.4
22102247
0 commit comments