NoReferencedTableError:Foreign key associated with column 'user.role_id' could not find ...

 在创建好相应的模型类后,运行该python程序后产生以下错误

NoReferencedTableError: Foreign key associated with column 'user.role_id' could not find table 'tbl_roles' 
with which to generate a foreign key to target column 'id'

 错误原因是外键找不到对应的表,检查后,发现__tablename__写成了__table__name,修正后如下,运行成功

class Role(db.Model):
    __tablename__ = 'tbl_roles'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(32), unique=True)

    users = db.relationship('User', backref='role')


class User(db.Model):
    __tablename__ = 'tbl_users'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    email = db.Column(db.String(128), unique=True)
    password = db.Column(db.String(128))
    role_id = db.Column(db.Integer, db.ForeignKey('tbl_roles.id'))

 

你可能感兴趣的:(NoReferencedTableError:Foreign key associated with column 'user.role_id' could not find ...)