I'm using a lot of werkzeug.local.LocalProxy objects in my Flask app. They are supposed to be perfect stand-ins for objects, but they aren't really, since they don't respond to type() or instanceof() correctly.
SQLAlchemy doesn't like them at all. If I make a LocalProxy to a SQLAlchemy record, SQLAlchemy considers it to be None. If I pass it a LocalProxy to a simpler type, it just says it's the wrong type.
Here's an example of Flask-SQLAlchemy having a bad time with LocalProxy.
How do you guys deal with this problem? Just call _get_current_object() a lot? It'd be pretty cool if SQLAlchemy or Flask-SQLAlchemy could automatically handle these LocalProxy objects more gracefully, especially considering Flask-Login uses them, and pretty much everybody uses that, right?
I'm considering adding this function to my project to deal with it, and wrapping any of my localproxies in it before passing them to sqlalchemy:
from werkzeug.local import LocalProxy
def real(obj):
if isinstance(obj, LocalProxy):
return obj._get_current_object()
return obj
It seems like you are trying to use LocalProxy objects in places where they are not directly compatible. One option you have is to use the real() function you defined to unwrap the LocalProxy objects before passing them to SQLAlchemy. This will allow SQLAlchemy to work with the underlying object rather than the proxy.
Another option would be to avoid using LocalProxy objects in places where they are not compatible. You can consider using regular objects or variables instead of LocalProxy objects in these cases.
It might also be helpful to report this issue to the Flask-SQLAlchemy or SQLAlchemy project, as you suggest, to see if they can improve compatibility with LocalProxy objects in future releases.