Skip to content

Commit b844a9c

Browse files
committed
doc amendments for issue #1232
Change-Id: I2508e632af75edec63df9c49bddfe07e9b8d71cc
1 parent fa35ee9 commit b844a9c

4 files changed

Lines changed: 157 additions & 45 deletions

File tree

alembic/op.pyi

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ def add_column(
8080
The :meth:`.Operations.add_column` method typically corresponds
8181
to the SQL command "ALTER TABLE... ADD COLUMN". Within the scope
8282
of this command, the column's name, datatype, nullability,
83-
and optional server-generated defaults may be indicated.
83+
and optional server-generated defaults may be indicated. Options
84+
also exist for control of single-column primary key and foreign key
85+
constraints to be generated.
8486
8587
.. note::
8688
8789
Not all contraint types may be indicated with this directive.
88-
PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
90+
NOT NULL, FOREIGN KEY, and CHECK are honored, PRIMARY KEY
91+
is conditionally honored, UNIQUE
8992
is currently not.
9093
91-
.. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
92-
emitted within :meth:`.Operations.add_column`.
93-
9494
As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
9595
parameters are **ignored**:
9696
@@ -99,6 +99,39 @@ def add_column(
9999
* :paramref:`~sqlalchemy.schema.Column.index` - use the
100100
:meth:`.Operations.create_index` method
101101
102+
**PRIMARY KEY support**
103+
104+
The provided :class:`~sqlalchemy.schema.Column` object may include a
105+
``primary_key=True`` directive, indicating the column intends to be
106+
part of a primary key constraint. However by default, the inline
107+
"PRIMARY KEY" directive is not emitted, and it's assumed that a
108+
separate :meth:`.Operations.create_primary_key` directive will be used
109+
to create this constraint, which may potentially include other columns
110+
as well as have an explicit name. To instead render an inline
111+
"PRIMARY KEY" directive, the
112+
:paramref:`.AddColumnOp.inline_primary_key` parameter may be indicated
113+
at the same time as the ``primary_key`` parameter (both are needed)::
114+
115+
from alembic import op
116+
from sqlalchemy import Column, INTEGER
117+
118+
op.add_column(
119+
"organization",
120+
Column("id", INTEGER, primary_key=True),
121+
inline_primary_key=True
122+
)
123+
124+
The ``primary_key=True`` parameter on
125+
:class:`~sqlalchemy.schema.Column` also indicates behaviors such as
126+
using the ``SERIAL`` datatype with the PostgreSQL database, which is
127+
why two separate, independent parameters are provided to support all
128+
combinations.
129+
130+
.. versionadded:: 1.18.4 Added
131+
:paramref:`.AddColumnOp.inline_primary_key`
132+
to control use of the ``PRIMARY KEY`` inline directive.
133+
134+
**FOREIGN KEY support**
102135
103136
The provided :class:`~sqlalchemy.schema.Column` object may include a
104137
:class:`~sqlalchemy.schema.ForeignKey` constraint directive,
@@ -128,6 +161,8 @@ def add_column(
128161
inline_references=True,
129162
)
130163
164+
**Indicating server side defaults**
165+
131166
The column argument passed to :meth:`.Operations.add_column` is a
132167
:class:`~sqlalchemy.schema.Column` construct, used in the same way it's
133168
used in SQLAlchemy. In particular, values or functions to be indicated
@@ -151,23 +186,25 @@ def add_column(
151186
quoting of the schema outside of the default behavior, use
152187
the SQLAlchemy construct
153188
:class:`~sqlalchemy.sql.elements.quoted_name`.
154-
:param if_not_exists: If True, adds IF NOT EXISTS operator
189+
:param if_not_exists: If True, adds ``IF NOT EXISTS`` operator
155190
when creating the new column for compatible dialects
156191
157192
.. versionadded:: 1.16.0
158193
159-
:param inline_references: If True, renders FOREIGN KEY constraints
160-
inline within the ADD COLUMN directive using REFERENCES syntax,
161-
rather than as a separate ALTER TABLE ADD CONSTRAINT statement.
162-
This is supported by PostgreSQL, Oracle, MySQL 5.7+, and
194+
:param inline_references: If True, renders ``FOREIGN KEY`` constraints
195+
inline within the ``ADD COLUMN`` directive using ``REFERENCES``
196+
syntax, rather than as a separate ``ALTER TABLE ADD CONSTRAINT``
197+
statement. This is supported by PostgreSQL, Oracle, MySQL 5.7+, and
163198
MariaDB 10.5+.
164199
165200
.. versionadded:: 1.18.2
166201
167-
:param inline_primary_key: If True, renders the PRIMARY KEY constraint
168-
inline within the ADD COLUMN directive, rather than rendering it
169-
separately. This is a purely syntactic option and should only be
170-
used for single-column primary keys.
202+
:param inline_primary_key: If True, renders the ``PRIMARY KEY`` phrase
203+
inline within the ``ADD COLUMN`` directive. When not present or
204+
False, ``PRIMARY KEY`` is not emitted; it is assumed that the
205+
migration script will include an additional
206+
:meth:`.Operations.create_primary_key` directive to create a full
207+
primary key constraint.
171208
172209
.. versionadded:: 1.18.4
173210

alembic/operations/base.py

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -646,17 +646,17 @@ def add_column(
646646
The :meth:`.Operations.add_column` method typically corresponds
647647
to the SQL command "ALTER TABLE... ADD COLUMN". Within the scope
648648
of this command, the column's name, datatype, nullability,
649-
and optional server-generated defaults may be indicated.
649+
and optional server-generated defaults may be indicated. Options
650+
also exist for control of single-column primary key and foreign key
651+
constraints to be generated.
650652
651653
.. note::
652654
653655
Not all contraint types may be indicated with this directive.
654-
PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
656+
NOT NULL, FOREIGN KEY, and CHECK are honored, PRIMARY KEY
657+
is conditionally honored, UNIQUE
655658
is currently not.
656659
657-
.. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
658-
emitted within :meth:`.Operations.add_column`.
659-
660660
As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
661661
parameters are **ignored**:
662662
@@ -665,6 +665,39 @@ def add_column(
665665
* :paramref:`~sqlalchemy.schema.Column.index` - use the
666666
:meth:`.Operations.create_index` method
667667
668+
**PRIMARY KEY support**
669+
670+
The provided :class:`~sqlalchemy.schema.Column` object may include a
671+
``primary_key=True`` directive, indicating the column intends to be
672+
part of a primary key constraint. However by default, the inline
673+
"PRIMARY KEY" directive is not emitted, and it's assumed that a
674+
separate :meth:`.Operations.create_primary_key` directive will be used
675+
to create this constraint, which may potentially include other columns
676+
as well as have an explicit name. To instead render an inline
677+
"PRIMARY KEY" directive, the
678+
:paramref:`.AddColumnOp.inline_primary_key` parameter may be indicated
679+
at the same time as the ``primary_key`` parameter (both are needed)::
680+
681+
from alembic import op
682+
from sqlalchemy import Column, INTEGER
683+
684+
op.add_column(
685+
"organization",
686+
Column("id", INTEGER, primary_key=True),
687+
inline_primary_key=True
688+
)
689+
690+
The ``primary_key=True`` parameter on
691+
:class:`~sqlalchemy.schema.Column` also indicates behaviors such as
692+
using the ``SERIAL`` datatype with the PostgreSQL database, which is
693+
why two separate, independent parameters are provided to support all
694+
combinations.
695+
696+
.. versionadded:: 1.18.4 Added
697+
:paramref:`.AddColumnOp.inline_primary_key`
698+
to control use of the ``PRIMARY KEY`` inline directive.
699+
700+
**FOREIGN KEY support**
668701
669702
The provided :class:`~sqlalchemy.schema.Column` object may include a
670703
:class:`~sqlalchemy.schema.ForeignKey` constraint directive,
@@ -694,6 +727,8 @@ def add_column(
694727
inline_references=True,
695728
)
696729
730+
**Indicating server side defaults**
731+
697732
The column argument passed to :meth:`.Operations.add_column` is a
698733
:class:`~sqlalchemy.schema.Column` construct, used in the same way it's
699734
used in SQLAlchemy. In particular, values or functions to be indicated
@@ -717,23 +752,25 @@ def add_column(
717752
quoting of the schema outside of the default behavior, use
718753
the SQLAlchemy construct
719754
:class:`~sqlalchemy.sql.elements.quoted_name`.
720-
:param if_not_exists: If True, adds IF NOT EXISTS operator
755+
:param if_not_exists: If True, adds ``IF NOT EXISTS`` operator
721756
when creating the new column for compatible dialects
722757
723758
.. versionadded:: 1.16.0
724759
725-
:param inline_references: If True, renders FOREIGN KEY constraints
726-
inline within the ADD COLUMN directive using REFERENCES syntax,
727-
rather than as a separate ALTER TABLE ADD CONSTRAINT statement.
728-
This is supported by PostgreSQL, Oracle, MySQL 5.7+, and
760+
:param inline_references: If True, renders ``FOREIGN KEY`` constraints
761+
inline within the ``ADD COLUMN`` directive using ``REFERENCES``
762+
syntax, rather than as a separate ``ALTER TABLE ADD CONSTRAINT``
763+
statement. This is supported by PostgreSQL, Oracle, MySQL 5.7+, and
729764
MariaDB 10.5+.
730765
731766
.. versionadded:: 1.18.2
732767
733-
:param inline_primary_key: If True, renders the PRIMARY KEY constraint
734-
inline within the ADD COLUMN directive, rather than rendering it
735-
separately. This is a purely syntactic option and should only be
736-
used for single-column primary keys.
768+
:param inline_primary_key: If True, renders the ``PRIMARY KEY`` phrase
769+
inline within the ``ADD COLUMN`` directive. When not present or
770+
False, ``PRIMARY KEY`` is not emitted; it is assumed that the
771+
migration script will include an additional
772+
:meth:`.Operations.create_primary_key` directive to create a full
773+
primary key constraint.
737774
738775
.. versionadded:: 1.18.4
739776

alembic/operations/ops.py

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

docs/build/changelog.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ Changelog
6868
.. note::
6969

7070
As of version 1.18.4, this behavior has been amended to be opt-in
71-
via the new ``inline_primary_key`` parameter to
72-
:meth:`.Operations.add_column`, rather than occurring automatically
73-
when ``primary_key=True`` is set on the :class:`.Column` object.
71+
via the new :paramref:`.Operations.add_column.inline_primary_key`
72+
parameter to :meth:`.Operations.add_column`, rather than occurring
73+
automatically when ``primary_key=True`` is set on the
74+
:class:`.Column` object.
7475

7576
.. change::
7677
:tags: bug, typing

0 commit comments

Comments
 (0)