Skip to content Skip to sidebar Skip to footer

Flask-sqlalchemy - Model Has No Attribute 'foreign_keys'

I have 3 models created with Flask-SQLalchemy: User, Role, UserRole role.py: class Role( ActiveRecord, db.Model ): __tablename__ = 'roles' # Schema id = db.Column(

Solution 1:

it's a little tough here because you are using some unknown base classes like "ActiveRecord" and such. However, it looks pretty much like that "secondary" argument is wrong:

class User( db.Model, ActiveRecord ):

    __tablename__ = "users"

    #   Schema
    id = db.Column( db.Integer, primary_key = True )
    email = db.Column( db.String( 90 ), unique = True )
    password = db.Column( db.String( 64 ) )

    #   Relations
    roles = db.relationship( "Role", secondary = "UserRole", \
        backref = db.backref( "users" ) )

secondary needs to refer to a Table object, not a mapped class, if by string name then it would be "user_roles":

roles = db.relationship( "Role", secondary = "user_roles", \
        backref = db.backref( "users" ) )

Post a Comment for "Flask-sqlalchemy - Model Has No Attribute 'foreign_keys'"