Skip to content

Commit 29acb02

Browse files
committed
allowing the index type to be specified in the Column definition
1 parent 6bcfa66 commit 29acb02

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

piccolo/columns/base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
from piccolo.utils.warnings import colored_warning
3232

3333
if t.TYPE_CHECKING: # pragma: no cover
34-
from piccolo.table import Table
3534
from piccolo.columns.column_types import ForeignKey
35+
from piccolo.query.methods.create_index import IndexMethod
36+
from piccolo.table import Table
3637

3738

3839
class OnDelete(str, Enum):
@@ -119,7 +120,7 @@ class ColumnMeta:
119120
primary: bool = False
120121
key: bool = False
121122
unique: bool = False
122-
index: bool = False
123+
index: t.Union[IndexMethod, bool] = False
123124
required: bool = False
124125

125126
# Used for representing the table in migrations and the playground.
@@ -231,8 +232,10 @@ class Column(Selectable):
231232
If set, a unique contraint will be added to the column.
232233
233234
:param index:
234-
If set, the an index is created for the column, which can improve
235-
the speed of selects, but can slow down inserts.
235+
Whether an index is created for the column, which can improve
236+
the speed of selects, but can slow down inserts. If set to True, the
237+
default index type is created (b-tree). If False, no index is created.
238+
If an IndexMethod value is provided, that type of index is created.
236239
237240
:param required:
238241
This isn't used by the database - it's to indicate to other tools that
@@ -249,7 +252,7 @@ def __init__(
249252
primary: bool = False,
250253
key: bool = False,
251254
unique: bool = False,
252-
index: bool = False,
255+
index: t.Union[IndexMethod, bool] = False,
253256
required: bool = False,
254257
**kwargs,
255258
) -> None:

piccolo/query/methods/create.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from piccolo.query.base import Query
55
from piccolo.querystring import QueryString
6-
from piccolo.query.methods.create_index import CreateIndex
6+
from piccolo.query.methods.create_index import CreateIndex, IndexMethod
77

88
if t.TYPE_CHECKING: # pragma: no cover
99
from piccolo.table import Table
@@ -44,13 +44,22 @@ def querystrings(self) -> t.Sequence[QueryString]:
4444

4545
create_indexes: t.List[QueryString] = []
4646
for column in columns:
47-
if column._meta.index:
48-
create_indexes.extend(
49-
CreateIndex(
50-
table=self.table,
51-
columns=[column],
52-
if_not_exists=self.if_not_exists,
53-
).querystrings
54-
)
47+
if column._meta.index is False:
48+
continue
49+
elif column._meta.index is True:
50+
method = IndexMethod.btree
51+
elif isinstance(column._meta.index, IndexMethod):
52+
method = column._meta.index
53+
else:
54+
raise ValueError("Unrecognised value for column index")
55+
56+
create_indexes.extend(
57+
CreateIndex(
58+
table=self.table,
59+
columns=[column],
60+
method=method,
61+
if_not_exists=self.if_not_exists,
62+
).querystrings
63+
)
5564

5665
return [create_table] + create_indexes

0 commit comments

Comments
 (0)