Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] When doing Annotated[str, Indexed(str, unique=True)] the uniqueness is not enforced #1036

Open
guyzmo opened this issue Oct 2, 2024 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@guyzmo
Copy link

guyzmo commented Oct 2, 2024

Describe the bug

As described in this issue
And discussed in this issue
And implemented in this PR
And according to beanie's documentation as well as pymongo's documentation

we can declare indexes with the following syntax:

class User
    user_id: Annotated[str, Indexed(str)]

To Reproduce

But if we want to declare an index with the uniqueness property, it is not enforced:

class User
    user_id: Annotated[str, Indexed(str, unique=True)]

user_1 = User(user_id: "foo").create()
user_2 = User(user_id: user_1.id).create()

assert len(User.find_all(User.user_id == user_1.user_id)) == 2

Expected behavior

There should be only one user with a given user_id, not two:

class User
    user_id: Annotated[str, Indexed(str, unique=True)]

user_1 = User(user_id: "foo").create()
user_2 = User(user_id: user_1.id).create()

assert len(User.find_all(User.user_id == user_1.id)) == 1

Additional context

I'm still running an older version of beanie (v1.11.6), but it looks like this issue is still actual. Cf discord.

@guyzmo
Copy link
Author

guyzmo commented Oct 3, 2024

It looks like this issue has been fixed in the PR #762. Found it thanks to the community on discord 🫶

Though, I'm not up to date, so I'm upgrading and come back here to tell whether it fixed my issue or not. I hope it does 🤞

@MrEarle
Copy link
Contributor

MrEarle commented Oct 6, 2024

When used with Annotated, you shouldn't provide the type to the Indexed function. Try this instead:

class User
    user_id: Annotated[str, Indexed(unique=True)]

@staticxterm
Copy link

Hi @MrEarle, thank you for the explanation. I just looked at the code as was having a hard time to understand why we can't also provide the type in this case also, but I see now that this is by design.
I believe this ticket should still stay open as at least we should clarify this usage in the Indexes docs, to avoid any future confusion.

@staticxterm staticxterm added the documentation Improvements or additions to documentation label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants