flask中db.create_all()创建表失败,报错:该表不存在

在用flask模型来创建数据表的时候报错:ProgrammingError: (_mysql_exceptions.ProgrammingError) (1146, "Table 'tflask.user' doesn't exist") [SQL: u'SELECT count(%s) AS count_1 \nFROM user'] [parameters: ('*',)] (Background on this error at: http://sqlalche.me/e/f405)

该错误意思是需要创建的表不存在,查看了代码:

db = SQLAlchemy(app)
db.init_app(app)
db.create_all()

class User(db.Model, UserMixin):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    username = db.Column(db.String(20), nullable=False)
    password_hash = db.Column(db.String(30), nullable=False)

db.create_all()放在了模型User之前,所以没有创建到该表,应该把其放在要创建表的后面

db = SQLAlchemy(app)
db.init_app(app)

class User(db.Model, UserMixin):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    username = db.Column(db.String(20), nullable=False)
    password_hash = db.Column(db.String(30), nullable=False)

db.create_all()

然后查看数据库,该表创建成功

你可能感兴趣的:(flask基础)