Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))
like image 821
Joe Avatar asked Jul 03 '18 00:07

Joe


People also ask

How do I move a table in Flask?

The normal migration process goes as follows: You will make some changes to your models in your Python source code. You will then run flask db migrate to generate a new database migration for these changes. You will finally apply the changes to the database by running flask db upgrade .

What is meant by Flask migrate?

Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are provided as command-line arguments under the flask db command.


1 Answers

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
like image 119
Joe Avatar answered Oct 27 '22 17:10

Joe