Flask-Migrate not detecting tables

Multi tool use
Multi tool use


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:



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





what do your models look like?
– wgwz
Jul 3 at 0:40





@wgwz my models.py file is too big to share the whole thing but i've included only a relevant example. It's not an issue with my models as I can use db.create_all() in a shell successfully
– Joe
Jul 3 at 0:46





ah ha. i think its likely because you created the db object in multiple places. you’ll be better off using the approach to instantiating flask extensions project/__init__.py than in the manage.py.
– wgwz
Jul 3 at 0:50





id guess you are overriding the db instance in your manage.py
– wgwz
Jul 3 at 0:51





it is important to show the models.py bc the import statements could explain why its not working
– wgwz
Jul 3 at 0:52




1 Answer
1



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

VvJXAckGn9AgQdOjg PSqpHDybpnqJM1u m7vtVRkLgyZ6sqFV3Cb17FXZn1QMly,mXHct5A7sMqSYfRzRRHeJI bOE
nOUX9,uOAZUkPw,0o4GYcXUAE2M8yfXy

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications