Refty / mongo-thingy

:leaves: Powerful schema-less ODM for MongoDB and Python (sync + async)
https://mongo-thingy.readthedocs.io
MIT License
68 stars 12 forks source link

mongo_thingy doesn't create indexes #29

Closed pacud closed 5 years ago

pacud commented 5 years ago

Hey,

I have an API that uses mongo_thingy for its models in which I'm trying to add indexes the files look like this :

from mongo_thingy import Thingy
from mongo_thingy.versioned import Versioned

class Model(Versioned, Thingy):
    [...]

Model.add_index("key")

___all___ = ["Model"]

And I can't find the indexes in the database when launching my API. I've tried putting a breakpoing inside the create_indexes function and when printing the registry I have :

-> for cls in registry:
(Pdb) registry
[<class 'thingy.Thingy'>, <class 'thingy.DatabaseThingy'>, <class 'mongo_thingy.Thingy'>]

So I'm thinking that this is the root cause, given that my model is not listed in the registry, there will be no index created for it.

How do I fix this ?

ramnes commented 5 years ago

I cannot reproduce. :thinking:

$ cat << EOF | python3
from mongo_thingy import Thingy, registry
from mongo_thingy.versioned import Versioned

class Model(Versioned, Thingy):
    pass

Model.add_index("key")

print(Model in registry)
EOF
True
ramnes commented 5 years ago

I suppose that you have your model and your create_indexes in two different files, and that the problem here simply is that your model hasn't been imported yet and thus its file hasn't been executed neither.

What if you add import your.model.module before running create_indexes?

pacud commented 5 years ago

That was exactly it.

Thanks.