Skip to content

Commit

Permalink
Fix postgresql_include in create_index
Browse files Browse the repository at this point in the history
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 🎫`513`. Pull request courtesy Steven Bronson.

Copied from e01041b and 174cb4b.

Fixes: #874
Closes: #875
Pull-request: #875
Pull-request-sha: 2f9b518

Change-Id: I68cc2a75dafd3297c5121c5125ff9c463c74f777
  • Loading branch information
sbronson authored and zzzeek committed Aug 23, 2021
1 parent 6aad686 commit 84bbbeb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions alembic/ddl/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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
Expand Down Expand Up @@ -71,6 +72,16 @@ class PostgresqlImpl(DefaultImpl):
)
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:
Expand Down
9 changes: 9 additions & 0 deletions docs/build/unreleased/874.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. 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.
13 changes: 13 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ def test_create_index_postgresql_concurrently(self):
"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)
Expand Down

0 comments on commit 84bbbeb

Please sign in to comment.