-
Notifications
You must be signed in to change notification settings - Fork 217
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] Link DBRefs do not preserve extra attributes when calling document.model_dump()
#1041
Comments
I would technically not characterize this as a bug as this was simply never implemented. My guess is that DBRef comes from PyMongo right? |
Well, Also, when creating a document with a link, you need to either call |
To further drive de discussion, Beanie showcases Links as a way to filter a document based on the linked document field. In my example, it would be: Todo.find(Todo.user.email == email, fetch_links=True) But this performs a lookup before filtering, and cannot take advantages of indices, which requires expensive colscan operations. This get harder for more complex real-world examples. A way to improve on this, is to have extra fields, like in the embedded document pattern, that can be indexed ( But the current implementation does not support this. |
I see. That may be very useful but would probably complicate the design or overall logic. But I do agree that at least this possibility to add extra fields is currently missing, and your proposed code in |
Well, it should be the responsibility of the developer to determine when to update the extra fields in the DBRef, as that is part of the business logic of their use cases. The advantages of embedded document pattern are to improve read performance, at the expense of write performance, which is usually rarer. This is a common (and recommended) pattern for MongoDB. Maybe the solution is not to add this functionality to Links, but have a new type Link-ish feature that represents an embedded document. |
Describe the bug
When dumping a model with links, the corresponding DBRef dicts don't keep extra attributes.
To Reproduce
Expected behavior
A clear and concise description of what you expected to happen.
I would expect that the serialization of the DBRef contains the extra fields it contains. This helps in having an extended reference pattern.
Additional context
Playing around with beanie's implementation, I got it working by modifying the
to_dict
method ofLink
:Edit: This would probably change how to do serialization with Pydantic. For example, in
build_validation
for Pydantic V2,if isinstance(v, dict) and v.keys() == {"id", "collection"}
is used to detect if the dict is a DBRef. Given thatkeys
can now contain more items, the strict equality would no longer hold.The text was updated successfully, but these errors were encountered: