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

switch_db does not dereference properly #1288

Open
trein opened this issue May 3, 2016 · 1 comment
Open

switch_db does not dereference properly #1288

trein opened this issue May 3, 2016 · 1 comment

Comments

@trein
Copy link

trein commented May 3, 2016

I found switch_db functionality really handy and decided to tried it out this weekend. The problem is that it seems ReferenceFields are not properly resolved while in switch_db context. Here is what I've tried so far:

class Agency(DocumentModel):
    agency_id = db.StringField()
    name = db.StringField()
    url = db.StringField()
    timezone = db.StringField()
    lang = db.StringField()
    phone = db.StringField()
    fare_url = db.StringField()

class Route(DocumentModel):
    agency = db.ReferenceField(Agency)
    route_id = db.StringField()
    short_name = db.StringField()
    long_name = db.StringField()
    route_type = db.StringField()
    desc = db.StringField()
    url = db.StringField()
    color = db.StringField()
    text_color = db.StringField()
def route(self, route_id):
    with switch_db(document_models.Route, self.db_name()) as Route:
        route = Route.objects.get(route_id=route_id)
        assert (isinstance(route.agency, document_models.Agency))
        return route

The assertion fails as route.agency returns a Dbref. However, if I remove completely swith_db and setup the initial database as self.db_name(), everything works as expected.

def route(self, route_id):
    route = document_models.Route.objects.get(route_id=route_id)
    assert (isinstance(route.agency, document_models.Agency))
    return route

I even tried several other cases, with and without select_related(). No luck.

@trein
Copy link
Author

trein commented Nov 14, 2016

Apparently that's not the focus of MongoEngine. I just don't understand why it provides a feature such as switch_db when it does not work properly with dereferencing. Please, correct me if I'm wrong, but the main reason people use MongoEngine is because of dereferencing. Otherwise, the use of straight up PyMongo would be a more suitable option I suppose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant