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] Issue with List[Link[Type]] when fetch_all_links is called #576

Closed
space55 opened this issue May 30, 2023 · 4 comments
Closed

[BUG] Issue with List[Link[Type]] when fetch_all_links is called #576

space55 opened this issue May 30, 2023 · 4 comments
Assignees
Labels

Comments

@space55
Copy link

space55 commented May 30, 2023

Describe the bug
As discussed in the Discord, when using List[Link[Type]], the following error is received:

  File "/Users/space/.local/share/virtualenvs/api-server-master-haddR1V9/lib/python3.10/site-packages/beanie/odm/documents.py", line 1060, in fetch_all_links
    await asyncio.gather(*coros)
  File "/Users/space/.local/share/virtualenvs/api-server-master-haddR1V9/lib/python3.10/site-packages/beanie/odm/documents.py", line 1051, in fetch_link
    values = await Link.fetch_list(ref_obj, fetch_links=True)
  File "/Users/space/.local/share/virtualenvs/api-server-master-haddR1V9/lib/python3.10/site-packages/beanie/odm/fields.py", line 217, in fetch_list
    fetched_models = await model_class.find(  # type: ignore
AttributeError: 'NoneType' object has no attribute 'find'

To Reproduce

import asyncio
from typing import Optional, List

from motor.motor_asyncio import AsyncIOMotorClient

from beanie import Document, Indexed, init_beanie, Link


class Bar(Document):
    value: str


class Foo(Document):
    name: str
    bars: Optional[List[Link[Bar]]] = []


async def example():
    client = AsyncIOMotorClient("mongodb://admin:admin@localhost:27017")
    await init_beanie(database=client.db_name, document_models=[Foo, Bar])

    bar = Bar(value="World")
    await bar.save()

    foo = Foo(name="Hello", bars=[bar])
    await foo.save()

    await foo.fetch_all_links()


if __name__ == "__main__":
    asyncio.run(example())

Expected behavior
Not this error

Additional context
Running with Motor, and the Optional is potentially important. I always recreate this with a completely fresh database running in docker. Please let me know if you have any issues reproducing this, or any other questions!

@roman-right roman-right added the bug Something isn't working label Jun 2, 2023
@roman-right roman-right self-assigned this Jun 2, 2023
@voelcker-itsc
Copy link

voelcker-itsc commented Jun 12, 2023

Encounterd the same problem for mandatory fields, if I used fetch_links=True in the find method.
A quick debug later, I guess the problem is in the fetch_list method if the data list is empty (optional field) or no link is a Link (already fetched). So model_class will never get set.

@roman-right
Copy link
Member

This will be fixed in #669

@roman-right roman-right removed the bug Something isn't working label Aug 21, 2023
@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the Stale label Sep 21, 2023
@github-actions
Copy link

github-actions bot commented Oct 5, 2023

This issue was closed because it has been stalled for 14 days with no activity.

@github-actions github-actions bot closed this as completed Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants