compiler.get_column_specification(column, **kw),
)
+ if column.primary_key:
+ text += " PRIMARY KEY"
+
const = " ".join(
compiler.process(constraint) for constraint in column.constraints
)
.. note::
- With the exception of NOT NULL constraints or single-column FOREIGN
- KEY constraints, other kinds of constraints such as PRIMARY KEY,
- UNIQUE or CHECK constraints **cannot** be generated using this
- method; for these constraints, refer to operations such as
- :meth:`.Operations.create_primary_key` and
- :meth:`.Operations.create_check_constraint`. In particular, the
- following :class:`~sqlalchemy.schema.Column` parameters are
- **ignored**:
-
- * :paramref:`~sqlalchemy.schema.Column.primary_key` - SQL databases
- typically do not support an ALTER operation that can add
- individual columns one at a time to an existing primary key
- constraint, therefore it's less ambiguous to use the
- :meth:`.Operations.create_primary_key` method, which assumes no
- existing primary key constraint is present.
+ Not all contraint types may be indicated with this directive.
+ PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
+ is currently not.
+
+ .. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
+ emitted within :meth:`.Operations.add_column`.
+
+ As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
+ parameters are **ignored**:
+
* :paramref:`~sqlalchemy.schema.Column.unique` - use the
:meth:`.Operations.create_unique_constraint` method
* :paramref:`~sqlalchemy.schema.Column.index` - use the
.. note::
- With the exception of NOT NULL constraints or single-column FOREIGN
- KEY constraints, other kinds of constraints such as PRIMARY KEY,
- UNIQUE or CHECK constraints **cannot** be generated using this
- method; for these constraints, refer to operations such as
- :meth:`.Operations.create_primary_key` and
- :meth:`.Operations.create_check_constraint`. In particular, the
- following :class:`~sqlalchemy.schema.Column` parameters are
- **ignored**:
-
- * :paramref:`~sqlalchemy.schema.Column.primary_key` - SQL databases
- typically do not support an ALTER operation that can add
- individual columns one at a time to an existing primary key
- constraint, therefore it's less ambiguous to use the
- :meth:`.Operations.create_primary_key` method, which assumes no
- existing primary key constraint is present.
+ Not all contraint types may be indicated with this directive.
+ PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
+ is currently not.
+
+ .. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
+ emitted within :meth:`.Operations.add_column`.
+
+ As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
+ parameters are **ignored**:
+
* :paramref:`~sqlalchemy.schema.Column.unique` - use the
:meth:`.Operations.create_unique_constraint` method
* :paramref:`~sqlalchemy.schema.Column.index` - use the
.. note::
- With the exception of NOT NULL constraints or single-column FOREIGN
- KEY constraints, other kinds of constraints such as PRIMARY KEY,
- UNIQUE or CHECK constraints **cannot** be generated using this
- method; for these constraints, refer to operations such as
- :meth:`.Operations.create_primary_key` and
- :meth:`.Operations.create_check_constraint`. In particular, the
- following :class:`~sqlalchemy.schema.Column` parameters are
- **ignored**:
-
- * :paramref:`~sqlalchemy.schema.Column.primary_key` - SQL databases
- typically do not support an ALTER operation that can add
- individual columns one at a time to an existing primary key
- constraint, therefore it's less ambiguous to use the
- :meth:`.Operations.create_primary_key` method, which assumes no
- existing primary key constraint is present.
+ Not all contraint types may be indicated with this directive.
+ PRIMARY KEY, NOT NULL, FOREIGN KEY, and CHECK are honored, UNIQUE
+ is currently not.
+
+ .. versionadded:: 1.18.2 Added support for PRIMARY KEY to be
+ emitted within :meth:`.Operations.add_column`.
+
+ As of 1.18.2, the following :class:`~sqlalchemy.schema.Column`
+ parameters are **ignored**:
+
* :paramref:`~sqlalchemy.schema.Column.unique` - use the
:meth:`.Operations.create_unique_constraint` method
* :paramref:`~sqlalchemy.schema.Column.index` - use the
--- /dev/null
+.. change::
+ :tags: usecase, operations
+ :tickets: 1232
+
+ The ``primary_key`` parameter on :class:`.Column` is now honored when
+ :meth:`.Operations.add_column` is used, and will emit the "PRIMARY KEY"
+ keyword inline within the ADD COLUMN directive. This is strictly a syntax
+ enhancement; no attempt is made to reconcile the column's primary key
+ status with any existing primary key constraint or particular backend
+ limitations on adding columns to the primary key.
def _datetime_server_default_fixture(self):
return func.current_timestamp()
- @exclusions.fails()
def test_drop_pk_col_readd_pk_col(self):
super().test_drop_pk_col_readd_pk_col()
def _datetime_server_default_fixture(self):
return func.current_timestamp()
- @exclusions.fails()
def test_drop_pk_col_readd_pk_col(self):
super().test_drop_pk_col_readd_pk_col()
'ALTER TABLE "some.schema".somename ADD COLUMN colname VARCHAR'
)
+ def test_add_column_primary_key(self):
+ context = op_fixture("postgresql")
+ op.add_column(
+ "somename",
+ Column("colname", String, primary_key=True),
+ )
+
+ context.assert_(
+ "ALTER TABLE somename ADD COLUMN colname "
+ "VARCHAR NOT NULL PRIMARY KEY"
+ )
+
def test_rename_table_schema_hard_quoting(self):
context = op_fixture("postgresql")
op.rename_table(
def test_col_w_pk_is_serial(self):
context = op_fixture("postgresql")
op.add_column("some_table", Column("q", Integer, primary_key=True))
- context.assert_("ALTER TABLE some_table ADD COLUMN q SERIAL NOT NULL")
+ context.assert_(
+ "ALTER TABLE some_table ADD COLUMN q SERIAL NOT NULL PRIMARY KEY"
+ )
def test_create_exclude_constraint(self):
context = op_fixture("postgresql")