Flask-SQLAlchemy - model has no attribute 'foreign_keys'

后端 未结 1 817
囚心锁ツ
囚心锁ツ 2021-02-05 03:55

I have 3 models created with Flask-SQLalchemy: User, Role, UserRole

user.py:

class Role( ActiveRecord, db.Model ):

    __tablename__ = \"roles\"

    #          


        
相关标签:
1条回答
  • 2021-02-05 04:30

    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" ) )
    
    0 讨论(0)
提交回复
热议问题