Skip to content

Commit 8c0e252

Browse files
zzzeekGerrit Code Review
authored andcommitted
Merge "render PRIMARY KEY in add_column" into main
2 parents 91a5116 + ee9a8e9 commit 8c0e252

8 files changed

Lines changed: 58 additions & 48 deletions

File tree

alembic/ddl/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ def add_column(
355355
compiler.get_column_specification(column, **kw),
356356
)
357357

358+
if column.primary_key:
359+
text += " PRIMARY KEY"
360+
358361
const = " ".join(
359362
compiler.process(constraint) for constraint in column.constraints
360363
)

alembic/op.pyi

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,16 @@ def add_column(
8282
8383
.. note::
8484
85-
With the exception of NOT NULL constraints or single-column FOREIGN
86-
KEY constraints, other kinds of constraints such as PRIMARY KEY,
87-
UNIQUE or CHECK constraints **cannot** be generated using this
88-
method; for these constraints, refer to operations such as
89-
:meth:`.Operations.create_primary_key` and
90-
:meth:`.Operations.create_check_constraint`. In particular, the
91-
following :class:`~sqlalchemy.schema.Column` parameters are
92-
**ignored**:
93-
94-
* :paramref:`~sqlalchemy.schema.Column.primary_key` - SQL databases
95-
typically do not support an ALTER operation that can add
96-
individual columns one at a time to an existing primary key
97-
constraint, therefore it's less ambiguous to use the
98-
:meth:`.Operations.create_primary_key` method, which assumes no
99-
existing primary key constraint is present.
85+
Not all contraint types may be indicated with this directive.
86+
PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
87+
is currently not.
88+
89+
.. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
90+
emitted within :meth:`.Operations.add_column`.
91+
92+
As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
93+
parameters are **ignored**:
94+
10095
* :paramref:`~sqlalchemy.schema.Column.unique` - use the
10196
:meth:`.Operations.create_unique_constraint` method
10297
* :paramref:`~sqlalchemy.schema.Column.index` - use the

alembic/operations/base.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -648,21 +648,16 @@ def add_column(
648648
649649
.. note::
650650
651-
With the exception of NOT NULL constraints or single-column FOREIGN
652-
KEY constraints, other kinds of constraints such as PRIMARY KEY,
653-
UNIQUE or CHECK constraints **cannot** be generated using this
654-
method; for these constraints, refer to operations such as
655-
:meth:`.Operations.create_primary_key` and
656-
:meth:`.Operations.create_check_constraint`. In particular, the
657-
following :class:`~sqlalchemy.schema.Column` parameters are
658-
**ignored**:
659-
660-
* :paramref:`~sqlalchemy.schema.Column.primary_key` - SQL databases
661-
typically do not support an ALTER operation that can add
662-
individual columns one at a time to an existing primary key
663-
constraint, therefore it's less ambiguous to use the
664-
:meth:`.Operations.create_primary_key` method, which assumes no
665-
existing primary key constraint is present.
651+
Not all contraint types may be indicated with this directive.
652+
PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
653+
is currently not.
654+
655+
.. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
656+
emitted within :meth:`.Operations.add_column`.
657+
658+
As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
659+
parameters are **ignored**:
660+
666661
* :paramref:`~sqlalchemy.schema.Column.unique` - use the
667662
:meth:`.Operations.create_unique_constraint` method
668663
* :paramref:`~sqlalchemy.schema.Column.index` - use the

alembic/operations/ops.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,21 +2115,16 @@ def add_column(
21152115
21162116
.. note::
21172117
2118-
With the exception of NOT NULL constraints or single-column FOREIGN
2119-
KEY constraints, other kinds of constraints such as PRIMARY KEY,
2120-
UNIQUE or CHECK constraints **cannot** be generated using this
2121-
method; for these constraints, refer to operations such as
2122-
:meth:`.Operations.create_primary_key` and
2123-
:meth:`.Operations.create_check_constraint`. In particular, the
2124-
following :class:`~sqlalchemy.schema.Column` parameters are
2125-
**ignored**:
2126-
2127-
* :paramref:`~sqlalchemy.schema.Column.primary_key` - SQL databases
2128-
typically do not support an ALTER operation that can add
2129-
individual columns one at a time to an existing primary key
2130-
constraint, therefore it's less ambiguous to use the
2131-
:meth:`.Operations.create_primary_key` method, which assumes no
2132-
existing primary key constraint is present.
2118+
Not all contraint types may be indicated with this directive.
2119+
PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
2120+
is currently not.
2121+
2122+
.. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
2123+
emitted within :meth:`.Operations.add_column`.
2124+
2125+
As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
2126+
parameters are **ignored**:
2127+
21332128
* :paramref:`~sqlalchemy.schema.Column.unique` - use the
21342129
:meth:`.Operations.create_unique_constraint` method
21352130
* :paramref:`~sqlalchemy.schema.Column.index` - use the

docs/build/unreleased/1232.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. change::
2+
:tags: usecase, operations
3+
:tickets: 1232
4+
5+
The ``primary_key`` parameter on :class:`.Column` is now honored when
6+
:meth:`.Operations.add_column` is used, and will emit the "PRIMARY KEY"
7+
keyword inline within the ADD COLUMN directive. This is strictly a syntax
8+
enhancement; no attempt is made to reconcile the column's primary key
9+
status with any existing primary key constraint or particular backend
10+
limitations on adding columns to the primary key.

tests/test_batch.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,6 @@ class BatchRoundTripMySQLTest(BatchRoundTripTest):
22912291
def _datetime_server_default_fixture(self):
22922292
return func.current_timestamp()
22932293

2294-
@exclusions.fails()
22952294
def test_drop_pk_col_readd_pk_col(self):
22962295
super().test_drop_pk_col_readd_pk_col()
22972296

@@ -2348,7 +2347,6 @@ def _native_boolean_fixture(self):
23482347
def _datetime_server_default_fixture(self):
23492348
return func.current_timestamp()
23502349

2351-
@exclusions.fails()
23522350
def test_drop_pk_col_readd_pk_col(self):
23532351
super().test_drop_pk_col_readd_pk_col()
23542352

tests/test_op.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ def test_add_column_schema_hard_quoting(self):
7171
'ALTER TABLE "some.schema".somename ADD COLUMN colname VARCHAR'
7272
)
7373

74+
def test_add_column_primary_key(self):
75+
context = op_fixture("postgresql")
76+
op.add_column(
77+
"somename",
78+
Column("colname", String, primary_key=True),
79+
)
80+
81+
context.assert_(
82+
"ALTER TABLE somename ADD COLUMN colname "
83+
"VARCHAR NOT NULL PRIMARY KEY"
84+
)
85+
7486
def test_rename_table_schema_hard_quoting(self):
7587
context = op_fixture("postgresql")
7688
op.rename_table(

tests/test_postgresql.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ def test_drop_column_if_exists(self):
188188
def test_col_w_pk_is_serial(self):
189189
context = op_fixture("postgresql")
190190
op.add_column("some_table", Column("q", Integer, primary_key=True))
191-
context.assert_("ALTER TABLE some_table ADD COLUMN q SERIAL NOT NULL")
191+
context.assert_(
192+
"ALTER TABLE some_table ADD COLUMN q SERIAL NOT NULL PRIMARY KEY"
193+
)
192194

193195
def test_create_exclude_constraint(self):
194196
context = op_fixture("postgresql")

0 commit comments

Comments
 (0)