BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
2.08k stars 219 forks source link

[BUG]`fetch_links=True` doesn't work with `View`s #1011

Open efrechette-rga opened 2 months ago

efrechette-rga commented 2 months ago

Using fetch_links=True with a View results in an error:

  ...
  File ".../beanie/odm/queries/find.py", line 1042, in __await__
    document = yield from self._find_one().__await__()  # type: ignore
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../beanie/odm/queries/find.py", line 998, in _find_one
    return await self.document_model.find_many(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../beanie/odm/queries/find.py", line 701, in first_or_none
    res = await self.limit(1).to_list()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../beanie/odm/queries/cursor.py", line 72, in to_list
    cursor = self.motor_cursor
             ^^^^^^^^^^^^^^^^^
  File ".../beanie/odm/queries/find.py", line 674, in motor_cursor
    ] = self.build_aggregation_pipeline()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../beanie/odm/queries/find.py", line 623, in build_aggregation_pipeline
    ] = construct_lookup_queries(
        ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../beanie/odm/utils/find.py", line 32, in construct_lookup_queries
    database_major_version=cls._database_major_version,
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../pydantic/_internal/_model_construction.py", line 237, in __getattr__
    raise AttributeError(item)
AttributeError: _database_major_version

To Reproduce

class MyView(View):
    some_field: str
    some_link: Link[MyOtherModel]

    class Settings:
        source = MyModel
        pipeline = ...

MyView.find_one(MyView.some_field == "hi", fetch_links=True)

Expected behavior View.find_one(..., fetch_links=True) works exactly like it does for Documents.

github-actions[bot] commented 1 month ago

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

marconadar commented 1 month ago

+1 fetch_links=True does not fully populate the linked documents but Object { id: "668ea3dda0a3e171ae3fbd71", collection: "users" } in the linked field of the linking document.