BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
2.02k stars 215 forks source link

[BUG] Inheritance doesn't work for backlinks? #965

Closed ldorigo closed 3 months ago

ldorigo commented 3 months ago

Describe the bug Since https://beanie-odm.dev/tutorial/inheritance/#relations says that "Linked documents will be resolved into the respective classes", I also expected this to be the case for backlinks. However, that does not seem to be the case?

To Reproduce

class Foo(Document):
    bar: Link[Bar]

    class Settings:
         is_root = True

class SubFoo1(Document):
    new_prop_1: str

class SubFoo2(Document):
    new_prop_2: int

class Bar:
   foos: list[BackLink[Foo]] = Field(json_schema_extra={"original_field": "bar"})

bar = Bar(foos=[])
await bar.insert()

sf1 = SubFoo1(bar=bar, new_prop_1="test")
await sf1.insert()

sf2 = SubFoo2(bar=bar, new_prop_2=2)
await sf2.insert()

bar = await Bar.get(bar.id)

Expected behavior In the above, I expected bar.get() to return an object with foos set to [SubFoo1(...), SubFoo2(...)]; however I get a list of [Foo(...), Foo(...)].

Additional context Aside from a proper fix, is there any temporary workaround? I spent a long time designing my new models/logic around this only to find out that the inheritance for backlinks is broken..

ldorigo commented 3 months ago

Update: I'm an idiot, I forgot to add the child objects to init_beanie.