Scille / umongo

sync/async MongoDB ODM, yes.
MIT License
448 stars 63 forks source link

`find` with `GenericReferenceField` does not return the document. #362

Open jbkoh opened 3 years ago

jbkoh commented 3 years ago

Hi there,

I have documents with GenericReferenceField but when the fields are used for find, it doesn't return the matched document. I guess this is a bug. Here's the regenerator:

import asyncio
from motor.motor_asyncio import AsyncIOMotorClient
from marshmallow.exceptions import ValidationError
from umongo import Document
from umongo.fields import StringField, GenericReferenceField
from umongo.frameworks import MotorAsyncIOInstance

instance = MotorAsyncIOInstance()
DBNAME = 'testdb'
SAME_VALUE = "aaa"

@instance.register
class ReferenceDoc(Document):
    some_key = StringField(unique=True)

@instance.register
class TestDoc(Document):
    ref1_key = GenericReferenceField(required=True)
    some_key = StringField()

async def get_ref(val):
    ref_doc = ReferenceDoc(some_key=val)
    try:
        await ref_doc.commit()
    except ValidationError:
        ref_doc = await ReferenceDoc.find_one({"some_key": val})
    return ref_doc

async def main():
    loop = asyncio.get_event_loop()
    conn = AsyncIOMotorClient(io_loop=loop)
    db = conn.get_database(DBNAME)
    instance.set_db(db)
    await TestDoc.ensure_indexes()
    await ReferenceDoc.ensure_indexes()

    ref1 = await get_ref("111")

    doc_shape = {
        "ref1_key": ref1,
        "some_key": SAME_VALUE,
    }

    doc1 = TestDoc(**doc_shape)
    await doc1.commit()

    found_doc = await TestDoc.find_one(doc_shape)
    assert found_doc, "THE SAME DOC NOT FOUND"

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

Versions