使用 flask_migrate 进行迁移时,运行烧瓶数据库 migrate -m 后生成.db文件"user"但没有创建表?



我正在使用flask_sqlalchemy创建一个用户表,并尝试使用flask_migrate进行迁移。但是没有创建用户表。 .db文件已在项目目录中生成,但在迁移/版本中未生成版本。 如何解决这个问题?

我关注了这些文章,但没有找到解决方案。以下是文章:- https://flask-migrate.readthedocs.io/en/latest/https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database


from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app,db)
from app import routes

我在终端上得到了这个输出 烧瓶数据库迁移 -m '用户'

INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.env] No changes in schema detected.
and expected output was :
(venv) $ flask db migrate -m "users table"
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'user'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_user_email' on '['email']'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_user_username' on '['username']'
Generating /home/miguel/microblog/migrations/versions/e517276bb1c2_users_table.py ... done

我刚刚遇到了同样的问题。如果您查看__init__.py底部的import语句,则您没有添加"模型"模块,因此它不知道您创建的新数据模型。

这是本教程中此时__init__.py的样子:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from config import Config

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models

最新更新