【问题标题】:Flask-Security error "User object has no attribute roles'Flask-Security 错误“用户对象没有属性角色”
【发布时间】:2026-02-13 15:00:01
【问题描述】:

这是我第一次使用 Flask-Security,我似乎无法成功创建用户。

每次尝试时,我都会收到“用户对象没有属性角色”错误。

以下是相关代码: https://gist.github.com/ianseyer/f08d3f47471588e79022

编辑: 意识到这可能是因为我没有__tablename__ = 'roles',但这给了我这个错误:

sqlalchemy.exc.ArgumentError: Could not determine join condition between parent/child     tables on relationship User.role.  Specify a 'primaryjoin' expression.  If 'secondary' is     present, 'secondaryjoin' is needed as well.

【问题讨论】:

    标签: flask sqlalchemy flask-sqlalchemy flask-security


    【解决方案1】:

    你称它为role。改变

    role = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic'))
    

    roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic'))
    

    【讨论】: