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] BackLink field value replacing with link object on save() #1006

Open
virrius opened this issue Aug 20, 2024 · 0 comments
Open

[BUG] BackLink field value replacing with link object on save() #1006

virrius opened this issue Aug 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@virrius
Copy link

virrius commented Aug 20, 2024

Describe the bug
When modifying a parameter and subsequently saving the model using .save(), I noticed an issue where a previously fetched BackLink field reverted its value from the fetched content back to a reference object.
Further use of the object model after saving may lead to errors.

To Reproduce

import asyncio

from beanie import BackLink, Document, Link, init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import Field


class Task(Document):
    subtasks: list[BackLink["SubTask"]] = Field(json_schema_extra={"original_field": "related_task_id"})
    n: int = 0

    class Settings:
        max_nesting_depths_per_field = {
            "subtasks": 1  # Nesting depth for a specific field
        }


class SubTask(Document):
    related_task_id: Link[Task]
    index: int = 0


async def main():
    db = AsyncIOMotorClient(
        'mongodb://admin:[email protected]/?retryWrites=true&w=majority',
        serverSelectionTimeoutMS=5000
    )['beanie_trouble_test']
    await init_beanie(db, document_models=[Task, SubTask])

    # preparing
    task = await Task().save()
    await SubTask(related_task_id=task.id, index=1).save()
    await SubTask(related_task_id=task.id, index=2).save()
    await SubTask(related_task_id=task.id, index=3).save()

    # testing
    task = await Task.get(task.id, fetch_links=True)
    print("fetched task: ", task)

    print("list of subtasks:")
    for sub in task.subtasks:
        print(sub)

    task.n = 100
    await task.save()
    print("task after saving: ", task)
    print("list of subtasks:")
    for sub in task.subtasks:
        print(sub)


if __name__ == '__main__':
    asyncio.run(main())

logs:

fetched task:  id=ObjectId('66c498fb13d341db6da587bc') revision_id=None subtasks=[SubTask(id=ObjectId('66c498fb13d341db6da587bd'), revision_id=None, related_task_id=<beanie.odm.fields.Link object at 0x000002A7DE947610>, index=1), SubTask(id=ObjectId('66c498fb13d341db6da587be'), revision_id=None, related_task_id=<beanie.odm.fields.Link object at 0x000002A7DE947650>, index=2), SubTask(id=ObjectId('66c498fb13d341db6da587bf'), revision_id=None, related_task_id=<beanie.odm.fields.Link object at 0x000002A7DE946890>, index=3)] n=0
list of subtasks:
id=ObjectId('66c498fb13d341db6da587bd') revision_id=None related_task_id=<beanie.odm.fields.Link object at 0x000002A7DE947610> index=1
id=ObjectId('66c498fb13d341db6da587be') revision_id=None related_task_id=<beanie.odm.fields.Link object at 0x000002A7DE947650> index=2
id=ObjectId('66c498fb13d341db6da587bf') revision_id=None related_task_id=<beanie.odm.fields.Link object at 0x000002A7DE946890> index=3
task after saving:  id=ObjectId('66c498fb13d341db6da587bc') revision_id=None subtasks=[<beanie.odm.fields.BackLink object at 0x000002A7DE947210>] n=100
list of subtasks:
<beanie.odm.fields.BackLink object at 0x000002A7DE947210>

Expected behavior
I guess, saving should either keep content unchanged or fetch it fully with updated data
Additional context
beanie==1.26.0
pydantic==2.8.2

@virrius virrius changed the title [BUG] BackLink field value replacing with link url object on save() [BUG] BackLink field value replacing with link object on save() Aug 20, 2024
@adeelsohailahmed adeelsohailahmed added the bug Something isn't working label Aug 21, 2024
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

2 participants