Flask-Migrate No Changes Detected to Schema on fir

2020-06-04 03:59发布

问题:

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

I've created two tables in 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

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

and 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)

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.

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.

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.

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.

UPDATE

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

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

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.

回答1:

Pls make sure that you already import your models (Tutor, Student, ...) before your migrate.init_app(app, db)



回答2:

I encountered this problem and solved it by importing my models at env.py in the migrations folder right after the following comments

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata

from app.models import Student, Tutor


回答3:

Well, I encountered the same problem following Miguel Grinberg tutorial.

Previously I created the tables using the shell calling

db.create_all()

So, I thougth to drop the tables

db.drop_all()

and trying the migrate command again, it worked as expected:

Roberto@MyPC MINGW64 /e/Projects/Flask/flasky ((5c))
$ flask db migrate -m "initial migration - Role Users"
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'roles'
INFO  [alembic.autogenerate.compare] Detected added table 'users'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_users_username' on '['username']'
Generating E:\Projects\Flask\flasky\migrations\versions\4de323c9c089_initial_migration_role_users.py ... done

After that, I used flask-migrate to re-create the tables

$ flask db upgrade
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> b641ee80a60d, initial migration - Role Users


回答4:

THis is how I solved the problem in my case:

  1. Importa the migrate model: from flask_migrate import Migrate
  2. Initiate Migrate class: migrate = Migrate(app, db)
  3. Comment db.create_all()
  4. Drop yout database now => DROP DATABASE db_name;
  5. Create it again => CREATE DATABSE db_name OWNER owner_name;
  6. Export you flask entry file => export FLASK_APP=name_app.py
  7. Run flask db migrate

Note: the 6th step should be used in case you may get this error : Error: Could not locate a Flask application

Hope this will help someone.