Skip to content

Nested Links are saved as embedded documents instead of references #1235

@iamgoeldhruv

Description

@iamgoeldhruv

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions