BeanieODM / beanie

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

[BUG] Link not working in find queries on fields with alias #945

Open rexa222 opened 3 weeks ago

rexa222 commented 3 weeks ago

Explanation Imagine an alias is set for a Link field (which it's value will be DBRef), when a find filter is used on the id of that DBRef to get a record, it does not work. However, if the field name in Document class is directly set to the previous class alias, it works perfectly.

Documents Example

class LinkDoc(Document):
    foo: str = 'foo'

class Doc(Document):
    field: Link[LinkDoc] = Field(alias='alias')

class DirectDoc(Document):
    alias: Link[LinkDoc]

Example Record in DB

_id: 6666cfcd915ab3ed5ff6155a
alias: DBRef('LinkDoc', '6666a7eca6fa9e0ac7a286b4')

Occurred Behavior

>>>  await Doc.find_one(Doc.field == ObjectId('6666a7eca6fa9e0ac7a286b4')
None
>>> await DirectDoc.find_one(Doc.field == ObjectId('6666a7eca6fa9e0ac7a286b4')
DirectDoc(id=ObjectId('6666cfcd915ab3ed5ff6155a'), revision_id=None, field=<beanie.odm.fields.Link object at 0x000002A731C38FD0>)