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

How do you do relations between documents in beanie? #8

Closed
nickleman opened this issue Mar 25, 2021 · 3 comments
Closed

How do you do relations between documents in beanie? #8

nickleman opened this issue Mar 25, 2021 · 3 comments

Comments

@nickleman
Copy link

Is there a way to automate relationships between documents in beanie or do you have to do it manually? If manually, are we able to access the ObjectId parameter of the document or should we just ensure that we have an index on a unique field? Also, can we set the unique field of an index to assist with this? I didn't see anything in the documentation about support of the unique flag of indexes. This may actually be multiple issues and let me know and I can split it up if you'd prefer.

@roman-right
Copy link
Member

Hello,

Yes, it is assumed to do it manually.
Every document contains id field - it is ObjectId (PydanticObjectId to be fair). You can use it to set the relations.
It is not documented. Nice catch - I'll clarify this.
Also, you can create a unique index. I'll show in example:

class DocumentTestModelWithIndex(Document):
    test_int: int
    test_list: List[SubDocument]
    test_str: str

    class Collection:
        indexes = [
            IndexModel(
                [("test_str", pymongo.ASCENDING)],
                unique=True,
            ),
        ]

It can be used then to map the objects too.
All the IndexModel parameters can be found here - https://pymongo.readthedocs.io/en/stable/api/pymongo/operations.html#pymongo.operations.IndexModel

I'll add an example of unique usage to the doc too. Thank you :-)

@nickleman
Copy link
Author

Sounds good, thank you.

@roman-right
Copy link
Member

Welcome!
If you will need any help, ask here or in discord.

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

No branches or pull requests

2 participants