Flask-Migrate在首次迁移时未检测到对架构的更改 [英] Flask-Migrate No Changes Detected to Schema on first migration

查看:245
本文介绍了Flask-Migrate在首次迁移时未检测到对架构的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Flask与Flask-SQLAlchemy和Flask-Migrate一起使用来创建应用程序,但是,当我尝试创建迁移时,什么也没发生.

I'm using Flask with Flask-SQLAlchemy and Flask-Migrate to create an application, however when I try to create a migration nothing happens.

我在app/models.py中创建了两个表:

from flask import current_app
from . import db

class Student(db.Model):
    __tablename__ = 'students'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, nullable=False)
    password_hash = db.Column(db.String(128))

    def __init__(self, **kwargs):
        super(Student, self).__init__(**kwargs)

    def __repr__(self):
        return '<Tutor {}>' % self.id

class Tutor(db.Model):
    __tablename__ = 'tutors'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))

    def __init__(self, **kwargs):
        super(Tutor, self).__init__(**kwargs)
    def __repr__(self):
        return '<Student %r>' % self.id

然后我也有app/__init__.py,其代码如下:

Then I also have app/__init__.py with the following code:

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

#from .models import User, Task, Project, UserProject

from config import config

bootstrap = Bootstrap()
db = SQLAlchemy()
migrate = Migrate()

def create_app(config_name='default'):
    #print config_name.name
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    db.init_app(app)
    migrate.init_app(app, db)

    ## Register the main blueprint for main app functionality
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

app.py:

import os
from app import create_app, db
from app.models import Tutor, Student

app = create_app('default')

@app.shell_context_processor
def make_shell_context():
    return dict(db=db, Tutor=Tutor, Student=Student)

我可以毫无问题地运行flask db init,它会创建以下目录的迁移目录和所有必需的文件:

I can run flask db init with no problem and it creates the migrations directory and all necessary files with the following output:

Creating directory /Users/Jasmine/projects/flask/flask-tutoring/migrations ... done
Creating directory /Users/Jasmine/projects/flask/flask-tutoring/migrations/versions ... done
Generating /Users/Jasmine/projects/flask/flask-tutoring/migrations/script.py.mako ... done
Generating /Users/Jasmine/projects/flask/flask-tutoring/migrations/env.py ... done
Generating /Users/Jasmine/projects/flask/flask-tutoring/migrations/README ... done
Generating /Users/Jasmine/projects/flask/flask-tutoring/migrations/alembic.ini ... done
Please edit configuration/connection/logging settings in '/Users/Jasmine/projects/flask/flask-tutoring/migrations/alembic.ini' before proceeding.

,但是当我尝试运行flask db migrate时,alembic无法检测到我在app/models.py中有表.我得到以下输出:

but when I try and run flask db migrate alembic can't detect that I've got tables in app/models.py. I get the following output:

INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.env] No changes in schema detected.

没有创建迁移脚本,就像models.py不存在一样.

There is no migration script created, its as though models.py doesn't exist.

很抱歉,如果这是一个重复的问题,但是我找不到另一个示例,该示例的第一个迁移失败并且完全没有创建迁移脚本.

Apologies if this is a repeated question, but I can't find another example where its the first migration that fails and no migration script at all is created.

我尝试通过在外壳中运行db.drop_all()来检查是否已在某处创建了表,但这似乎不是问题.

I've tried checking if there is already a table created somewhere by running db.drop_all() in the shell but that doesn't seem to be the problem.

我想出了一种自行解决此问题的方法,但希望更好地了解其工作原理.

I figured out a way to solve this on my own but would like a better understanding of why this worked.

我将app.py重命名为flasktutor.py,然后重新运行export FLASK_APP='flasktutor.py'.随后,迁移工作完美.

I re-named app.py to flasktutor.py and re-ran export FLASK_APP='flasktutor.py'. Subsequently the migration worked perfectly.

请有人可以解释为什么当文件被命名为app.py而我使用export FLASK_APP='app.py'时,迁移没有将更改注册到架构中.

Please could someone explain why when the file was called app.py and I used export FLASK_APP='app.py' the migration did not register changes to the schema.

推荐答案

请确保您已经在导入migrate.init_app(app, db)

这篇关于Flask-Migrate在首次迁移时未检测到对架构的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆