I have a project with the following structure in the src
directory:
├── migrations
├── .envrc
├── requirements.txt
├── alembic.ini
├── __init__.py
├── flask_app
│ ├── model.py (db and migrate objects declared here)
│ ├── app.py (from .model import db, migrate)
│ ├── landing
│ │ ├── landing_templates
│ │ ├── models.py (from flask_app.model import db)
│ │ ├── forms.py
│ │ └── views.py
│ ├── config.py
│ ├── __init__.py
│ ├── user
│ │ ├── user_templates
│ │ ├── models.py (from flask_app.model import db)
│ │ └── views.py
│ ├── templates
├── alembic
├── run_flask_app.py
When I run the line below while in the src
directory (which is in my python path):
flask --app 'flask_app.app:create_app("flask_app.config.DevelopmentConfig")' db migrate
Flask Migrate doesn’t detect any models and no migration script is generated. (ie INFO [alembic.env] No changes in schema detected.
).
However, when I change the imports in the models.py
files to from src.flask_app.model import db
the models are detected and a migration script is generated.
When I deploy the src
directory contents are deployed, but not the the directory itself, so I don’t think the the from src.flask_app.model import db
is an option. I had used this structure in another project and used Flask_Script for migrations, but Flask_Script is no longer maintained. I’d like to move to the modern tool, but I can’t figure out what my problem is.
Well, changing the import in app.py
to from flask_app.model import db, migrate
solved this. I think it made the names space between the app and the models the same. However, I still don’t know why importing from src.flask_app.model import db
in the model files worked.