Skip to content

Commit f255f33

Browse files
committed
exposing if_not_exists argument in create_index
1 parent 31f4646 commit f255f33

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

piccolo/apps/migrations/auto/migration_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ async def _run_alter_columns(self, backwards=False):
415415
column._meta._name = column_name
416416
await _Table.drop_index([column]).run()
417417
await _Table.create_index(
418-
[column], method=index_method
418+
[column], method=index_method, if_not_exists=True
419419
).run()
420420
else:
421421
# If the index value has changed, then we are either
@@ -427,7 +427,9 @@ async def _run_alter_columns(self, backwards=False):
427427
kwargs = (
428428
{"method": index_method} if index_method else {}
429429
)
430-
await _Table.create_index([column], **kwargs).run()
430+
await _Table.create_index(
431+
[column], if_not_exists=True, **kwargs
432+
).run()
431433
else:
432434
await _Table.drop_index([column]).run()
433435

piccolo/table.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,20 @@ def create_index(
623623
cls,
624624
columns: t.List[t.Union[Column, str]],
625625
method: IndexMethod = IndexMethod.btree,
626+
if_not_exists: bool = False,
626627
) -> CreateIndex:
627628
"""
628629
Create a table index. If multiple columns are specified, this refers
629630
to a multicolumn index, rather than multiple single column indexes.
630631
631632
await Band.create_index([Band.name]).run()
632633
"""
633-
return CreateIndex(table=cls, columns=columns, method=method)
634+
return CreateIndex(
635+
table=cls,
636+
columns=columns,
637+
method=method,
638+
if_not_exists=if_not_exists,
639+
)
634640

635641
@classmethod
636642
def drop_index(

0 commit comments

Comments
 (0)