BeanieODM / beanie

Asynchronous Python ODM for MongoDB

Home Page:http://beanie-odm.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Fetching Links in view raises AttributeError: _database_major_version

sheoak opened this issue · comments

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.

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

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