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

[BUG] updating/setting a nested parameter inside another does not understand alias and stores "new" attribute. #369

Closed
IGJAmpere opened this issue Oct 10, 2022 · 2 comments
Labels

Comments

@IGJAmpere
Copy link

Bug description

Basically, the only moment where the alias seems to be properly managed is for the Document. The attributes of nested pydantic models cannot be declared through the attribute name.

It is also important to consider that linters will be unable to understand the alias, but they do understand the attribute.

Bug reproduction

import asyncio
from typing import Optional
from beanie import Document, init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import Field, BaseModel, BaseConfig


class B(BaseModel):
    b_thing: int = Field(alias="bThing")

    class Config(BaseConfig):
        allow_population_by_field_name: bool = True


class A(Document):
    a_thing: int = Field(alias="aThing")
    thing: Optional[B]

    class Settings:
        name = "as"


async def main():

    client = AsyncIOMotorClient("mongodb://localhost:27017")

    db_name = client.db_name  # type: ignore
    await init_beanie(database=db_name, document_models=[A])

    await A.find_all().delete()

    await A(a_thing=3, thing=B(b_thing=3)).insert()

    # should understand that `b_thing` is to be transformed into  `bThing` in the DB.
    await A.find_one().set({A.thing.b_thing: 4})


if __name__ == "__main__":

    asyncio.run(main())

Expected behaviour

As the alias is respected for the main Document, it is expected that the same happens for nested objects. But this does not happen.

Hopefully this can get fixed. Thank you!

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the Stale label Dec 20, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2023

This issue was closed because it has been stalled for 14 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant