BeanieODM / beanie

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

[BUG] beanie 1.15.3 find_one always return None #415

Closed hd10180 closed 1 year ago

hd10180 commented 1 year ago

Describe the bug i use conda install latest beanie(1.15.3) and i found my code went wrong. after i defined my model, i use model.find_one and i always get None return, it seems like find_one is broken @roman-right

To Reproduce

class UserEntity(Document):

    username: str
    email: Optional[Union[EmailStr, None]]
    password: bytes

logger.error(
    UserEntity.find_one(
        UserEntity.username == login_obj.account
    ).get_filter_query()
)
# output: {'username': 'myusername'}
user = await UserEntity.find_one(UserEntity.username == login_obj.account)
logger.error(user)
# output: None

Expected behavior return an userEntity

Additional context after i reinstall beanie=1.13.0, all the things work by the way, beanie's release version on github went confuse, from 1.15.1 to 0.15.3

image

thanks your work.

roman-right commented 1 year ago

Thank you for the issue.

Here is the full-working example with find_one:

import asyncio

from motor.motor_asyncio import AsyncIOMotorClient

from beanie import Document, init_beanie

class UserEntity(Document):
    username: str

async def main():
    cli = AsyncIOMotorClient("mongodb://test:test@localhost:27017")
    db = cli["test_find_one"]
    await init_beanie(db, document_models=[UserEntity])

    await UserEntity(username="JohnDoe").insert()

    user = await UserEntity.find_one(UserEntity.username == "JohnDoe")

    print(user)
    # id=ObjectId('63721443e8e4e6cc80fba311') revision_id=None username='JohnDoe'

asyncio.run(main())

You probably use the Collection inner class which is not supported since 1.15, please use Settings instead, if so.

I'll fix the GH releases, thank you!

hd10180 commented 1 year ago

thanks