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] Inheritance fails silently for uninitialized collections #969

Open
ldorigo opened this issue Jul 12, 2024 · 6 comments
Open

[BUG] Inheritance fails silently for uninitialized collections #969

ldorigo opened this issue Jul 12, 2024 · 6 comments

Comments

@ldorigo
Copy link

ldorigo commented Jul 12, 2024

C.f. #965 for an example of where it bit me :-)

For normal documents there is an error message when an uninitialized object is used.

For documents that use inheritance, it looks like it just fails silently (using the baseclass instead of the child classes?)

Copy link
Contributor

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

@github-actions github-actions bot added the Stale label Aug 12, 2024
@adeelsohailahmed adeelsohailahmed added bug Something isn't working and removed Stale labels Aug 16, 2024
@staticxterm
Copy link

Hi @ldorigo,

I am unable to reproduce with the code provided in #965. Please provide a minimum reproducible example (MRE).

In the Beanie Relations documentation it states that 'BackLink' is only "virtual", it doesn't exist, so it can't be saved to DB.

@adeelsohailahmed I see that you've added the bug label. Do you perhaps have a MRE?

@adeelsohailahmed
Copy link
Contributor

@adeelsohailahmed I see that you've added the bug label. Do you perhaps have a MRE?

Hi @staticxterm, thank you for taking a look into this issue.

I haven't had the time to look into the issue and/or reproduce it myself. I just added the bug label to avoid the issue getting closed by the bot. In hindsight, it was perhaps not the best course of action.

@adeelsohailahmed adeelsohailahmed added action requested and removed bug Something isn't working labels Sep 3, 2024
Copy link
Contributor

github-actions bot commented Oct 4, 2024

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

@paulosestini
Copy link

I have also run into this issue.

When you don't initialize a child class of another document, you can still use it without any errors or warnings, but on mongo the documents originated from that class are silently assigned the _class_id of its parent, and this leads to problems when finding the child documents.

Here's a full example:

from beanie import init_beanie, Document
from motor.motor_asyncio import AsyncIOMotorClient
import asyncio


class Vehicle(Document):
    color: str
    class Settings:
        is_root = True


class Car(Vehicle):
    pass


class Motorcycle(Vehicle):
    pass


class Truck(Vehicle):
    pass


async def main():
    client = AsyncIOMotorClient("<your_url>")
    await init_beanie(
        database=client.get_database(), document_models=[Vehicle, Car]
    )  # Forgets to initialize Motorcycle and Truck

    car = Car(color="red")
    await car.save()

    motorcycle = Motorcycle(color="green")
    await motorcycle.save()  # runs without error/warning

    truck = Truck(color="blue")
    await truck.save()  # runs without error/warning

    vehicles = await Vehicle.find_all(with_children=True).to_list()

    print(vehicles)
    # prints [Car(color="red"), Vehicle(color="green"), Vehicle(color="blue")]

    motorcycles = await Motorcycle.find_all().to_list()
    print(motorcycles)
    # Both motorcycle and truck are read as motorcycles
    # prints [Motorcycle(color="green"), Motorcycle(color="blue")]

    trucks = await Truck.find_all().to_list()
    print(trucks)
    # Both motorcycle and truck are read as trucks
    # prints [Truck(color="green"), Truck(color="blue")]


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

For this example, when inspecting the saved documents on MongoDB, the car has attribute _class_id: 'Vehicle.Car', but the other two have _class_id: 'Vehicle'.

Should we have some warning for this?

Copy link
Contributor

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

@github-actions github-actions bot added the Stale label Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants