- 
                Notifications
    You must be signed in to change notification settings 
- Fork 261
Open
Description
Describe the bug
When using Link fields inside nested documents, Beanie saves them as embedded documents instead of storing them as references. Only the top-level Link is stored correctly as a reference.
To Reproduce
from beanie import Document, Link
class FilterItem(Document):
    name: str
    param1: object
    param2: object
    operator: str
    class Settings:
        name = "filter_items"
class Operation(Document):
    class Settings:
        name = "operations"
        is_root = True
class FilterOperation(Operation):
    filter: Link[FilterItem]
    class Settings(Operation.Settings):
        name = "filter_operation"
        is_root = False
class FilterGroupOperation(Operation):
    operator: Literal["and", "or", "anyOf"]
    operands: list[Link[Operation]]
    class Settings(Operation.Settings):
        name = "filter_group_operation"
        is_root = False
class FilterGroup(Document):
    operation: Link[Operation]
    class Settings:
        name = "filter_groups"
_ = Operation.model_rebuild()
_ = FilterOperation.model_rebuild()
_ = FilterGroupOperation.model_rebuild()
# Example creation
item = await FilterItem(name="f1", param1=1, param2=2, operator="eq").insert()
op = await FilterOperation(filter=item).insert()
group = await FilterGroupOperation(operator="and", operands=[op]).insert()
fg = await FilterGroup(operation=group).insert()
Expected behavior
It should insert various nested operations as link but its embedding entire document there
ADR-007
Metadata
Metadata
Assignees
Labels
No labels