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] Error while finding all the documents with Optional[Link[...]] = None #473

Closed
chabErch opened this issue Jan 11, 2023 Discussed in #328 · 2 comments
Closed

[BUG] Error while finding all the documents with Optional[Link[...]] = None #473

chabErch opened this issue Jan 11, 2023 Discussed in #328 · 2 comments
Assignees

Comments

@chabErch
Copy link

Discussed in #328

Originally posted by chabErch August 11, 2022
Hi ❤️!

I have the department model:

class Department(Document):
    name: str
    parent: Optional[Link['Department']] = None

And I want to get all the documents with parent == None:

departments = await Department.find(Department.parent == None, fetch_links=True).to_list()

And I get an error:

...
departments = await Department.find(Department.parent == None, fetch_links=True).to_list()
  File "/usr/local/lib/python3.9/site-packages/beanie/odm/queries/cursor.py", line 69, in to_list
    cursor = self.motor_cursor
  File "/usr/local/lib/python3.9/site-packages/beanie/odm/queries/find.py", line 595, in motor_cursor
    self.find_expressions[i] = convert_ids(
  File "/usr/local/lib/python3.9/site-packages/beanie/odm/utils/relations.py", line 61, in convert_ids
    and k.split(".")[1] == "id"

Do I miss something?

@roman-right roman-right self-assigned this Jan 11, 2023
@roman-right roman-right added the bug Something isn't working label Jan 11, 2023
@roman-right
Copy link
Member

Hi, @chabErch
Sorry for the delay. Could you please check if this is still reproducible?

I run this and it works:

import asyncio
from typing import Optional

from motor.motor_asyncio import AsyncIOMotorClient

from beanie import Document, Link, init_beanie


class Department(Document):
    name: str
    parent: Optional[Link['Department']] = None


async def main():
    client = AsyncIOMotorClient("mongodb://beanie:beanie@localhost:27017")
    await init_beanie(database=client.test, document_models=[Department])
    await Department(name="A").save()
    departments = await Department.find(Department.parent == None,
                                        fetch_links=True).to_list()
    print(departments)


asyncio.run(main())

@roman-right roman-right removed the bug Something isn't working label May 23, 2023
@chabErch
Copy link
Author

Yes, it works! I didn't even notice when everything got fixed.

Thank you ❤️!

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

No branches or pull requests

2 participants