BeanieODM / beanie

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

[BUG] Fetching Links in view raises AttributeError: _database_major_version #890

Closed sheoak closed 2 months ago

sheoak commented 4 months ago

Describe the bug When trying to fetching links in a view, AttributeError: _database_major_version is raised.

Side note: I couldn’t find any exemple in the documentation, in particular how to only fetch some fields from the linked document using the pipeline syntax.

To Reproduce

from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import Field
from beanie import Document, Indexed, init_beanie, Link, PydanticObjectId, View

class Person(Document):
    name: Indexed(str, unique=True)
    note: str = "Whatever"

class Car(Document):
    name: Indexed(str, unique=True)
    owners: list[Link[Person]]

class CarDashboard(View):
    # id: PydanticObjectId = Field(alias="_id")
    name: str
    owners: list[Link[Person]] = None

    class Settings:
        source = Car
        pipeline = [
            {
                "$project": {
                    # "_id": 1,
                    "name": 1,
                    "owners": 1,
                }
            },
        ]

client = AsyncIOMotorClient("mongodb://localhost:27017")
await init_beanie(database=client.db_name, document_models=[Car, Person, CarDashboard])

joe = Person(name="Joe")
await joe.insert()

jack = Person(name="Jack")
await jack.insert()

car = Car(name="SuperFast 2000", owners=[joe, jack])
await car.insert()

car2 = Car(name="SuperSlow 10", owners=[joe])
await car2.insert()

# without fetch_links, owners is empty
result = await CarDashboard.find_all(fetch_links=True).to_list()
print(result)

Expected behavior Retrieving a view with the links populated.

github-actions[bot] commented 3 months ago

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

github-actions[bot] commented 2 months ago

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