You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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']
The text was updated successfully, but these errors were encountered:
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 theDocument
from the database.To Reproduce
Output:
Random: at times, it works as expected; other times it might output:
Expected behavior
The text was updated successfully, but these errors were encountered: