flask_sqlalchemy object does not have attribute add when using flask_security

孤人 提交于 2020-05-28 07:44:29

问题


I am trying to add some roles/users with flask_security but run into this issue when I try to create users with the SQLAlchemySessionUserDatastore. So I first start by creating user_datastore like the guide

db = SQLAlchemy(app)
user_datastore = SQLAlchemySessionUserDatastore(db, User, Role)
security = Security(app, user_datastore)
user_datastore.create_user(email='test', password='passowrd')

this is where I deviate from the guide and try to just create a test user. Looks like the db = SQLAlchemy(app) is causing me issues.

    self.db.session.add(model)
AttributeError: 'SQLAlchemy' object has no attribute 'add'

回答1:


user_datastore = SQLAlchemySessionUserDatastore(db, User, Role)

The above line may be the culprit here.

My resolution is below: user_datastore = SQLAlchemySessionUserDatastore(db.session, User, Role)




回答2:


So if you were following the example here https://pythonhosted.org/Flask-Security/quickstart.html#id2 you will notice they use SQLAlchemySessionUserDatastore. Before getting to this point if you have been starting off with just flask_sqlalchemy you would of probably already had a db session. So when I narrowed it down to the object SQLAlchemy not having add attribute I tried to use the sessionmaker. In which I was responded with db session already exists and now I have 2 sessions.

Long story short reading some docs you should use SQLAlchemyUserDatastore instead!



来源:https://stackoverflow.com/questions/46616260/flask-sqlalchemy-object-does-not-have-attribute-add-when-using-flask-security

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!