Skip to content

Conversation

@mikelei8291
Copy link

I was trying to create two indexes on the same field for my model:

class Order(OrderInit, Document):
    created_at: Annotated[
        AwareDatetime,
        Field(default_factory=lambda: datetime.now(UTC))
    ]
    status: Annotated[OrderStatus, Indexed()]

    class Settings:
        indexes = [
            IndexModel(
                "created_at", name="processing_expire_30", expireAfterSeconds=30, partialFilterExpression={"status": "processing"}
            ),
            IndexModel(
                "created_at", name="unpaid_expire_900", expireAfterSeconds=900, partialFilterExpression={"status": "unpaid"}
            )
        ]

However, the current implementation will overwrite the first index definition with the second one because they have the same dict key if the key is index.fields. I changed the key to index.name so by providing a different name than the default one, the indexes will no longer overwrite each others.

Also I was trying to make it work with Annotated too:

class Order(OrderInit, Document):
    created_at: Annotated[
        AwareDatetime,
        Indexed(name="processing_expire_30", expireAfterSeconds=30, partialFilterExpression={"status": "processing"}),
        Indexed(name="unpaid_expire_900", expireAfterSeconds=900, partialFilterExpression={"status": "unpaid"}),
        Field(default_factory=lambda: datetime.now(UTC))
    ]
    status: Annotated[OrderStatus, Indexed()]

but it would change the return type of get_index_attributes() so I didn't go further.

@staticxterm staticxterm requested a review from a team April 11, 2025 17:06
Copy link
Member

@adeelsohailahmed adeelsohailahmed left a comment

Choose a reason for hiding this comment

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

Hi @mikelei8291, thanks for spotting and fixing this bug. Your implementation looks sound to me. Could you please add a test for this case too?

@staticxterm staticxterm requested a review from a team April 11, 2025 19:24
Copy link
Member

@staticxterm staticxterm left a comment

Choose a reason for hiding this comment

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

Hi @mikelei8291, thank you for the PR.
As Adeel mentioned, please fix any failing tests and write some new ones with example models and indexes so that we have this behavior (what was previously a bug) covered with tests.

@github-actions
Copy link
Contributor

This PR is stale because it has been open 45 days with no activity.

@github-actions
Copy link
Contributor

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions bot added the Stale label Jul 25, 2025
@staticxterm
Copy link
Member

I am still working on this. It's tricky. Sometimes we want to merge the indexes, sometimes not.

@staticxterm staticxterm self-assigned this Aug 3, 2025
@staticxterm staticxterm removed the Stale label Aug 3, 2025
@staticxterm staticxterm force-pushed the multiple-indexes-fix branch from 26cc53c to 21d0e6f Compare August 19, 2025 21:11
@staticxterm staticxterm changed the title Fix multiple indexes of the same field will overwrite each other Index merging improvements Aug 19, 2025
@staticxterm staticxterm requested review from a team and adeelsohailahmed August 19, 2025 21:14
@staticxterm staticxterm force-pushed the multiple-indexes-fix branch from 21d0e6f to 1746baa Compare August 19, 2025 21:55
@staticxterm staticxterm force-pushed the multiple-indexes-fix branch from 1746baa to 2541da4 Compare August 20, 2025 20:33
@staticxterm staticxterm dismissed their stale review August 20, 2025 20:37

Comments not valid anymore.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 5, 2025

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions bot added the Stale label Oct 5, 2025
@staticxterm staticxterm removed the Stale label Oct 6, 2025
@github-actions
Copy link
Contributor

This PR is stale because it has been open 45 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants