简体   繁体   中英

Flask-Migrate not detecting tables

I have the following project structure:

project/__init__.py

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

db = SQLAlchemy()
migrate = Migrate()

def create_app():
    app = Flask(__name__)
    app.config.from_object(os.environ['APP_SETTINGS'])

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

    return app

run.py

from project import create_app
app = create_app()

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

manage.py

from flask_script import Manager
from flask_migrate import MigrateCommand
from project.models import *
from project import create_app


manager = Manager(create_app)
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()

Yet when I run the following commands, Flask-Migrate is not detecting any tables to be added.

python manage.py db init

Which outputs:

Creating directory $HOME/Project/migrations ... done
Creating directory $HOME/Project/migrations/versions ... done
Generating $HOME/Project/migrations/script.py.mako ... done
Generating $HOME/Project/migrations/env.py ... done
Generating $HOME/Project/migrations/README ... done
Generating $HOME/Project/migrations/alembic.ini ... done
Please edit configuration/connection/logging settings in
'$HOME/Project/migrations/alembic.ini' before proceeding.

and

python manage.py db migrate

Which only outputs:

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.

Why is Flask-Migrate with Alembic not detecting the Models and therefore creating the tables? Here's what I've tried:

  • Deleting the database, starting from nothing
  • Creating a custom db class inside the manage.py file, doesn't detect that
  • Googling every answer to this problem, found lots of similar questions but none of their solutions worked for me.

EDIT:

Here is an example of the models.py file

from flask import current_app
from project import db
from flask_login import UserMixin

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))

The solution was to import the models in the __init__.py file like so:

def create_app():
    app = Flask(__name__)
    app.config.from_object(os.environ['APP_SETTINGS'])

    from project import models

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

    return app

I had the same issue. The solution is pretty same as @joe's

In routes file

from models import User

api_user = Namespace('user', description='user related operations')

In project/__init__.py

def create_app():
    app = Flask(__name__)
    app.config.from_object(os.environ['APP_SETTINGS'])

    from project import models

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

    blueprint = Blueprint('api', __name__, url_prefix=url_prefix)
    api = Api(blueprint, doc='/documentation')  # ,doc=False

    app.register_blueprint(blueprint)
    api.add_namespace(api_user)

return app

when adding api_user namespace in create_app flask_migrate detected models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM