Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find with GenericReferenceField does not return the document. #362

Open
jbkoh opened this issue Sep 12, 2021 · 0 comments
Open

find with GenericReferenceField does not return the document. #362

jbkoh opened this issue Sep 12, 2021 · 0 comments

Comments

@jbkoh
Copy link

jbkoh commented Sep 12, 2021

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

  • mongodb: 4.2.11
  • umongo: 3.0.0
  • motor: 2.5.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant