Skip to content

Conversation

sinisaos
Copy link
Member

@sinisaos sinisaos commented May 23, 2025

Related to #1188. This is almost identical PR as for composite unique constraints, only it doesn't use custom ddl but modified Piccolo methods create_index and drop_index. Although it serves a different purpose, feel free to remove it if it represents too much code duplication and adds complexity, as adding and dropping a composite index can easily be done with manual migrations as described in the comment.

Usage is:

from piccolo.table import Table
from piccolo.columns import Varchar, Boolean
from piccolo.composite_index import CompositeIndex


class Task(Table):
    name = Varchar()
    completed = Boolean(default=False)
    composite_name_completed = CompositeIndex(["name", "completed"])

@sinisaos sinisaos added the enhancement New feature or request label May 27, 2025
@sinisaos sinisaos requested a review from dantownsend June 2, 2025 05:39
Copy link
Member

@dantownsend dantownsend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the API - using CompositeIndex.

I agree that using strings is just much easier than actual column references.

class Album(Table):
    name = Varchar()
    released = Boolean(default=False)
    name_released_idx = CompositeIndex(["name", "released"])

Is there a way to specify the index type?

It looks like B-tree, GiST, GIN, and BRIN can be used with multi-column indexes:

https://www.postgresql.org/docs/current/indexes-multicolumn.html

@sinisaos
Copy link
Member Author

@dantownsend Great idea. I'll try to implement it. Thanks

Comment on lines +1104 to +1105
@engines_only("postgres", "cockroach")
def test_add_table_with_composite_index(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new commit with the index type in CompositeIndex. I didn't write any test because for that we need to create an extension for each of those index types (except B-tree) either in Github workflows or in PostgresEngine. I tried it locally and it works. The test would be the same as this, except we add e.g. "index_type": IndexMethod.gin instead of the default "index_type": IndexMethod.btree and create the extension with

CREATE EXTENSION pg_trgm; 
CREATE EXTENSION btree_gin; 

for that index type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants