Flask迁移不会检测到model.py

时间:2018-06-30 01:36:55

标签: python flask sqlite flask-sqlalchemy alembic

我正在尝试在我的工作目录中运行flask migrate db,但它没有使用我在models.py中定义的模型 这是代码。

models.py

import sys
sys.path.append("../")

from Talks2 import db

class Talk(db.Model):
    presenter = db.Column(db.Text())
    talkType = db.Column(db.Text())
    desc = db.Column(db.Text(), primary_key=True)
    link = db.Column(db.Text())
    time = db.Column(db.Integer())

    def __repr__(self):
        return "Presenter: {}\nType: {}\nDescription:\n{}\nLink:  {}".format(self.presenter,self.talkType,self.desc,self.link)

routes.py

import sys
sys.path.append("../")

from flask import Flask, request, render_template
from Talks2 import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app,db)

from Talks2 import models

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/add", methods=["POST"])
def add():
    person = request.form["presenter"]
    ttype = request.form["type"]
    desc = request.form["desc"]
    link = request.form["link"]
    print(person, file=sys.stderr)
    print(ttype, file=sys.stderr)
    print(desc, file=sys.stderr)
    print(link, file=sys.stderr)
    return render_template("index.html")

if __name__ == "__main__":
    app.run()

需要什么更改才能正确生成脚本?

1 个答案:

答案 0 :(得分:-2)

要创建迁移存储库:

flask db init

生成迁移脚本(请务必进行检查和编辑,因为Alembic当前无法检测到您对模型所做的所有更改)

flask db migrate

要将迁移应用于数据库

flask db upgrade

要查看所有可用命令,请运行以下命令:

flask db --help

有关更多信息,请参见this official docs