-
Notifications
You must be signed in to change notification settings - Fork 219
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
Comments
This issue is stale because it has been open 30 days with no activity. |
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? |
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. |
This issue is stale because it has been open 30 days with no activity. |
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 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 Should we have some warning for this? |
This issue is stale because it has been open 30 days with no activity. |
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?)
The text was updated successfully, but these errors were encountered: