mongodbfastapifastapiusers

init_beanie cannot initialize some collections


Having a well described model and schema using Pydantic and Beanie syntax, there are some collections, represented by their Document classes, which are not been initialized by init_beanie function at the startup event of a Fastapi app.

Did someone know what could be the causes of such a behavior?

Fastapi-users set a very special class named User, which is one of the well initialized by the background Beanie engine. After that, I added my entire model which consists in several classes.

For example, from my product_category module:

from typing import Optional    
from beanie import Document, Indexed    

class ProductCategory(Document):
    category: Indexed(str, unique=True)
    description: Optional[str]

    class Settings:
        name = "product_categories"

From my product_subcategory module:

from typing import Optional    
from beanie import Document, Link    
from product_category import ProductCategory
    
class ProductSubcategory(Document):
    category_id: Link[ProductCategory]
    subcategory: str
    description: Optional[str]

    class Settings:
        name = "product_subcategories"

...and so. The outcome of init_beanie reflects an initialization of a collection named ProductCategory, not product_categories as I think it would happened, because of the Settings inner class with its property "name", and that's it.

Such a behavior is not documented, and that's why I assume I'm making something wrong. Can anyone know how to fix this?

Thanks in advance. Jorge Olmedo.


Solution

  • As far as I know, and using the information I can gather from a couple of Python enthusiasts and collaborators:

    ...issue was solved. It goes like this: It seems Beanie have some problems to follow relative paths at import clause. All I did was changed all my relative module paths for absolute module paths, and that's it.