diff --git a/app/__init__.py b/app/__init__.py index 390d029..a709b09 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -10,7 +10,9 @@ def create_app(): # Initialize extensions db.init_app(app) migrate.init_app(app, db) + with app.app_context(): + import_all_models() db.create_all() # Initialize Logger @@ -29,6 +31,19 @@ def create_app(): return app +def import_all_models(): + """ + Dynamically imports every module inside app/models/ + so that db.create_all() knows about all tables (User, etc). + """ + import pkgutil + import importlib + import app.models as models_package + + for _, module_name, _ in pkgutil.iter_modules(models_package.__path__): + importlib.import_module(f"app.models.{module_name}") + + def register_blueprints(app): from app.routes.auth import auth_bp from app.routes.user_routes import user_bp