BeanieODM / beanie

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

[BUG] Optional[Backlink] #702

Closed hgalytoby closed 1 year ago

hgalytoby commented 1 year ago

Describe the bug https://beanie-odm.dev/tutorial/relations/

Optional[BackLink[...]]= Field(original_field="...")
Optional[List[BackLink[...]]]= Field(original_field="...")

AttributeError: 'ModelField' object has no attribute 'json_schema_extra'

https://github.com/roman-right/beanie/blob/main/beanie/odm/utils/init.py#L276

To Reproduce

import asyncio
from typing import List, Optional

from beanie import Document, BackLink, Link, init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import Field

class House(Document):
    name: str
    door: Link["Door"]
    owners: List[Link["Person"]]

class Door(Document):
    height: int = 2
    width: int = 1
    house: BackLink[House] = Field(original_field="door")

class Person(Document):
    name: str
    house: Optional[List[BackLink[House]]] = Field(default=None, original_field="owners")

async def main():
    client = AsyncIOMotorClient(
        'mongodb://root:12345678@localhost:27017'
    )
    await init_beanie(
        database=client.Noah,
        document_models=[
            House,
            Door,
            Person
        ],
    )

if __name__ == '__main__':
    asyncio.run(main())

Expected behavior can work.

roman-right commented 1 year ago

Hi @hgalytoby, Thank yoi for the catch. Fixed in https://github.com/roman-right/beanie/pull/703 . Will be merged soon

hgalytoby commented 1 year ago

Hi @hgalytoby, Thank yoi for the catch. Fixed in #703 . Will be merged soon

Thank you for your fix.

roman-right commented 1 year ago

Published in 1.22.5. Please, try

hgalytoby commented 1 year ago

Published in 1.22.5. Please, try

Thank you for your assistance! The issue has been resolved for me. I appreciate your help in solving this problem.