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] search criteria not using pydantic Field alias to set the key. #937

Open
axiangcoding opened this issue May 23, 2024 · 8 comments
Open
Labels
bug Something isn't working

Comments

@axiangcoding
Copy link

axiangcoding commented May 23, 2024

Describe the bug

search criteria not using pydantic alias to set the key.

To Reproduce

class VehicleBaseInfo(BaseModel):
    unit_class: str = Field(alias="unitClass")
    
class VehiclesInDB(Document):
    vehicle_info: VehicleBaseInfo

VehiclesInDB.find(VehiclesInDB.vehicle_info.unit_class == "something")

log here:

"command": "{\"aggregate\": \"vehicles\", \"pipeline\": [{\"$match\": {\"$and\": ...{\"vehicle_info.unit_class\": \"xxx\"}...}

Expected behavior
expecting search criteria like {"vehicle_info.unitClass": "xxx"}

Additional context
None

@axiangcoding axiangcoding changed the title [BUG] search criteria not using pydantic alias to set the key. [BUG] search criteria not using pydantic Field alias to set the key. May 24, 2024
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 Jun 24, 2024
@axiangcoding
Copy link
Author

anyone can help with this issue?

@github-actions github-actions bot removed the Stale label Jun 25, 2024
@mhennin
Copy link

mhennin commented Jul 12, 2024

I'm hitting the same issue. Pydantic aliases work for the first level object variable but none of the children.

For example, if my aliases are converting between snake-case and camel-case, it only uses the alias on the first level.

class CollectionFoo(Document):
    root_field: Child = Field(alias="rootField")
class Child(BaseModel):
    child_field = Field(alias="childField")

x = CollectionFoo.find(CollectionFoo.root_field.child_field == "value")
print(x.find_expressions)

Result: [{'rootField.child_field': 'value'}]
Expecting: [{'rootField.childField': 'value'}]

@axiangcoding
Copy link
Author

Is this a problem related to pydantic or beanie? If this is a known issue, i think it is better write it down in the document

@mhennin
Copy link

mhennin commented Aug 5, 2024

This is not a problem with pydantic. I use pydantic extensively with other python projects using complex nested structures. Child names are correctly formatted to their name alias rules in serialization/deserialization. This only appears to be a problem with how Beanie is translating multi-level structures to mongo specific queries. It doesn't seem to honor the field aliases for 2nd level and down, only root level names.

Copy link
Contributor

github-actions bot commented Sep 5, 2024

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

@github-actions github-actions bot added the Stale label Sep 5, 2024
@mhennin
Copy link

mhennin commented Sep 5, 2024

Not stale. This is still an issue.

@07pepa 07pepa added bug Something isn't working and removed Stale labels Sep 5, 2024
@07pepa
Copy link
Member

07pepa commented Sep 5, 2024

looks like bug in beanie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants