Open dcrosta opened 6 years ago
Ah, I've found the issue. When running with flask run ...
, it looks like Flask loads the app module several times. In my particular case, I use a factory function to create the application
object, which ultimately results in init_social
being called several times. Here's a small reproducing case:
social.py
from flask import Flask
from models import db
from social_flask_sqlalchemy.models import init_social
def create_application():
app = Flask(__name__)
app.config["SOCIAL_AUTH_USER_MODEL"] = "models.User"
app.config["SOCIAL_AUTH_STORAGE"] = "social_flask_sqlalchemy.models.FlaskStorage"
init_social(app, db.session)
return app
application = create_application()
models.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(256))
name = db.Column(db.String(256))
email = db.Column(db.String(256))
Run with FLASK_APP=social flask run
to see the traceback.
I have, for now, worked around this by returning a singleton from my app factory, but this is a bit janky. If there was some way this could be fixed in social-auth itself, that would be easier for me or anyone else who uses factories like I do (which makes testing a lot nicer).
Specifically, this footgun is the result of init_social() always importing the module your USER_MODEL is in, even if (as I've just discovered) your app is trivial enough that you put your model(s) in the same module as the app itself...
In @dcrosta's example, I think all that was needed was an if __name__ == "__main__":
before the last line of social.py.
I'm trying to set up PSA with Flask & SQLAlchemy. I've gotten as far as calling
init_social
, but as soon as I try to access my app (runflask shell
or load a page inflask run
, etc), I get this error:I'm using:
As near as I can tell, there's only one actual definition of
uid
, which is at https://github.com/python-social-auth/social-app-flask-sqlalchemy/blob/master/social_flask_sqlalchemy/models.py#L80 -- everywhere else in the class hierarchy,uid
is assigned toNone
. But I'm hoping that there's something obvious I've missed here, or something you've run into before that can help.Thanks!