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] Order of elements in list[Link] is not preserved #527

Closed
joaquimcampos opened this issue Apr 4, 2023 · 3 comments
Closed

[BUG] Order of elements in list[Link] is not preserved #527

joaquimcampos opened this issue Apr 4, 2023 · 3 comments
Labels

Comments

@joaquimcampos
Copy link

joaquimcampos commented Apr 4, 2023

Describe the bug
Something strange happening: after inserting an element in a list[Link] and saving to the db, the order of the elements is not preserved when fetching the Document from the database.

To Reproduce

import asyncio

from beanie import Document, Link, init_beanie
from beanie.odm.fields import WriteRules
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase


class Item(Document):
    s: str

    class Settings:
        name = "items"


class User(Document):
    items: list[Link[Item]] | None = None

    class Settings:
        name = "users"
        use_state_management = True


async def main() -> None:
    cli = AsyncIOMotorClient("mongodb://beanie:beanie@localhost:27017")
    db = cli["test_id"]
    await init_beanie(
        database=db,
        document_models=[User, Item]  # type: ignore
    )

    new_item1 = Item(s="first")
    new_item3 = Item(s="third")
    new_user = User(items=[new_item1, new_item3])
    user_db = await new_user.insert(link_rule=WriteRules.WRITE)

    new_item2 = Item(s="second")
    item2_db = await new_item2.insert()
    item2 = await Item.get(item2_db.id)
    assert item2

    user = await User.get(user_db.id, fetch_links=True)
    assert user
    user.items.insert(1, item2)
    await user.save_changes()  # or user.save() or user.replace()

    user = await User.get(user_db.id, fetch_links=True)
    assert user
    assert len(user.items) == 3
    print([item.s for item in user.items])

    await cli.drop_database(db)

asyncio.run(main())

Output:
Random: at times, it works as expected; other times it might output:

['second', 'first', 'third']
['first', 'third', 'second']

Expected behavior

['first', 'second', 'third']
@joaquimcampos
Copy link
Author

I realized this was already mentioned in:
#414 (comment)
#414 (comment)

@github-actions
Copy link
Contributor

github-actions bot commented May 6, 2023

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

@github-actions github-actions bot added the Stale label May 6, 2023
@github-actions
Copy link
Contributor

This issue was closed because it has been stalled for 14 days with no activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant