closeio / flask-common

Some commonly shared code for Flask, MongoEngine, and Python apps
26 stars 4 forks source link

Fix `fetch_related`'s behavior for broken references. #81

Closed wojcikstefan closed 4 years ago

wojcikstefan commented 4 years ago

This is an attempt at fixing an issue where a broken reference of a ReferenceField is never resolved. I'm not 100% sure that it always makes sense to resolve broken references to Nones, but I think it's worth considering.

Example usage:

In [1]: c = Call.objects.order_by('-date_created').first()

In [2]: from flask_common.mongo.utils import fetch_related

In [3]: fetch_related([c], {'transferred_from': {'user': True}})

In [4]: c.transferred_from

In [5]: # ^ W/o the change in this PR, this would raise a `DoesNotExist`.
thomasst commented 4 years ago

@wojcikstefan seems like there is a relevant test failing still. Tag me again for review when it's fixed (and if you feel inclined feel free to add another test).

wojcikstefan commented 4 years ago

Hm, that test makes sense though... Calling fetch_related should not make the objects we call it on lose the IDs of their broken references. I think I opened this PR prematurely.


It could be an interesting idea to attach a "missing" sentinel value to these broken references which would:

But that's just a minor optimization and not a major behavioral change that I was going for here.