Fixed issue where usage of the PostgreSQL ``postgresql_include`` option
within a :meth:`.Operations.create_index` would raise a KeyError, as the
additional column(s) need to be added to the table object used by the
construct internally. The issue is equivalent to the SQL Server issue fixed
in :ticket:`513`. Pull request courtesy Steven Bronson.
Copied from
e01041b and
174cb4b.
Fixes: #874
Closes: #875
Pull-request: https://github.com/sqlalchemy/alembic/pull/875
Pull-request-sha:
2f9b51890a1296146bcea842dc8bbee53d1d11f5
Change-Id: I68cc2a75dafd3297c5121c5125ff9c463c74f777
from sqlalchemy.dialects.postgresql import BIGINT
from sqlalchemy.dialects.postgresql import ExcludeConstraint
from sqlalchemy.dialects.postgresql import INTEGER
+from sqlalchemy.schema import CreateIndex
from sqlalchemy.sql.elements import ColumnClause
from sqlalchemy.sql.elements import UnaryExpression
from sqlalchemy.types import NULLTYPE
)
identity_attrs_ignore = ("on_null", "order")
+ def create_index(self, index):
+ # this likely defaults to None if not present, so get()
+ # should normally not return the default value. being
+ # defensive in any case
+ postgresql_include = index.kwargs.get("postgresql_include", None) or ()
+ for col in postgresql_include:
+ if col not in index.table.c:
+ index.table.append_column(Column(col, sqltypes.NullType))
+ self._exec(CreateIndex(index))
+
def prep_table_for_batch(self, batch_impl, table):
for constraint in table.constraints:
--- /dev/null
+.. change::
+ :tags: bug, postgresql
+ :tickets: 874
+
+ Fixed issue where usage of the PostgreSQL ``postgresql_include`` option
+ within a :meth:`.Operations.create_index` would raise a KeyError, as the
+ additional column(s) need to be added to the table object used by the
+ construct internally. The issue is equivalent to the SQL Server issue fixed
+ in :ticket:`513`. Pull request courtesy Steven Bronson.
"CREATE INDEX CONCURRENTLY geocoded ON locations (coordinates)"
)
+ @config.requirements.sqlalchemy_14
+ def test_create_index_postgresql_include(self):
+ context = op_fixture("postgresql")
+ op.create_index(
+ "i", "t", ["c1", "c2"], unique=False, postgresql_include=["inc"]
+ )
+ context.assert_("CREATE INDEX i ON t (c1, c2) INCLUDE (inc)")
+
+ def test_create_index_postgresql_include_is_none(self):
+ context = op_fixture("postgresql")
+ op.create_index("i", "t", ["c1", "c2"], unique=False)
+ context.assert_("CREATE INDEX i ON t (c1, c2)")
+
def test_drop_index_postgresql_concurrently(self):
context = op_fixture("postgresql")
op.drop_index("geocoded", "locations", postgresql_concurrently=True)