From d201221fb1b365c20356c7f1a99cbd79efc84a77 Mon Sep 17 00:00:00 2001 From: Samir Boulahtit Date: Mon, 9 Feb 2026 21:16:42 +0100 Subject: [PATCH] fix(scripts): fix model registration and missing platform_id in init scripts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All init scripts (init_log_settings, create_default_content_pages, create_platform_pages, seed_email_templates) failed because they didn't register all SQLAlchemy model classes, causing mapper resolution errors for cross-module relationships (Platform→ContentPage, Platform→SubscriptionTier). Fixes: - Add full model registration loop to all 5 init scripts - Add platform_id (OMS) to content page creation (NOT NULL constraint) - Add missing db.commit() to create_platform_pages.py (pages were never persisted) - Add cms.models to init_production.py registration list Co-Authored-By: Claude Opus 4.6 --- scripts/create_default_content_pages.py | 29 +++++++++++++++++++ scripts/create_platform_pages.py | 38 +++++++++++++++++++++++++ scripts/init_log_settings.py | 20 +++++++++++-- scripts/init_production.py | 1 + scripts/seed_email_templates.py | 18 ++++++++++++ 5 files changed, 104 insertions(+), 2 deletions(-) diff --git a/scripts/create_default_content_pages.py b/scripts/create_default_content_pages.py index 02d6a5f0..a6815a51 100755 --- a/scripts/create_default_content_pages.py +++ b/scripts/create_default_content_pages.py @@ -36,8 +36,27 @@ sys.path.insert(0, str(project_root)) from sqlalchemy import select from sqlalchemy.orm import Session +# Register all models with SQLAlchemy so string-based relationships resolve +for _mod in [ + "app.modules.billing.models", + "app.modules.inventory.models", + "app.modules.cart.models", + "app.modules.messaging.models", + "app.modules.loyalty.models", + "app.modules.catalog.models", + "app.modules.customers.models", + "app.modules.orders.models", + "app.modules.marketplace.models", + "app.modules.cms.models", +]: + try: + __import__(_mod) + except ImportError: + pass + from app.core.database import SessionLocal from app.modules.cms.models import ContentPage +from app.modules.tenancy.models import Platform # ============================================================================ # DEFAULT PAGE CONTENT @@ -470,6 +489,15 @@ def create_default_pages(db: Session) -> None: print("Creating Default Platform Content Pages (CMS)") print("=" * 70 + "\n") + # Resolve OMS platform for platform_id + oms_platform = db.execute( + select(Platform).where(Platform.code == "oms") + ).scalar_one_or_none() + if not oms_platform: + print(" ⚠️ OMS platform not found. Run init_production.py first.") + return + platform_id = oms_platform.id + created_count = 0 skipped_count = 0 @@ -490,6 +518,7 @@ def create_default_pages(db: Session) -> None: # Create new platform default page page = ContentPage( + platform_id=platform_id, store_id=None, # Platform default slug=page_data["slug"], title=page_data["title"], diff --git a/scripts/create_platform_pages.py b/scripts/create_platform_pages.py index e5c67fde..a5e3b26a 100755 --- a/scripts/create_platform_pages.py +++ b/scripts/create_platform_pages.py @@ -23,8 +23,29 @@ from pathlib import Path project_root = Path(__file__).resolve().parent.parent sys.path.insert(0, str(project_root)) +# Register all models with SQLAlchemy so string-based relationships resolve +for _mod in [ + "app.modules.billing.models", + "app.modules.inventory.models", + "app.modules.cart.models", + "app.modules.messaging.models", + "app.modules.loyalty.models", + "app.modules.catalog.models", + "app.modules.customers.models", + "app.modules.orders.models", + "app.modules.marketplace.models", + "app.modules.cms.models", +]: + try: + __import__(_mod) + except ImportError: + pass + +from sqlalchemy import select + from app.core.database import SessionLocal from app.modules.cms.services import content_page_service +from app.modules.tenancy.models import Platform def create_platform_pages(): @@ -40,6 +61,15 @@ def create_platform_pages(): # Import ContentPage for checking existing pages from app.modules.cms.models import ContentPage + # Resolve OMS platform + oms_platform = db.execute( + select(Platform).where(Platform.code == "oms") + ).scalar_one_or_none() + if not oms_platform: + print(" ⚠️ OMS platform not found. Run init_production.py first.") + return + platform_id = oms_platform.id + # ======================================================================== # 1. PLATFORM HOMEPAGE # ======================================================================== @@ -59,6 +89,7 @@ def create_platform_pages(): try: homepage = content_page_service.create_page( db, + platform_id=platform_id, slug="platform_homepage", title="Welcome to Our Multi-Store Marketplace", content=""" @@ -95,6 +126,7 @@ def create_platform_pages(): try: about = content_page_service.create_page( db, + platform_id=platform_id, slug="about", title="About Us", content=""" @@ -151,6 +183,7 @@ def create_platform_pages(): try: faq = content_page_service.create_page( db, + platform_id=platform_id, slug="faq", title="Frequently Asked Questions", content=""" @@ -236,6 +269,7 @@ def create_platform_pages(): try: contact = content_page_service.create_page( db, + platform_id=platform_id, slug="contact", title="Contact Us", content=""" @@ -305,6 +339,7 @@ def create_platform_pages(): try: terms = content_page_service.create_page( db, + platform_id=platform_id, slug="terms", title="Terms of Service", content=""" @@ -397,6 +432,7 @@ def create_platform_pages(): try: privacy = content_page_service.create_page( db, + platform_id=platform_id, slug="privacy", title="Privacy Policy", content=""" @@ -477,6 +513,8 @@ def create_platform_pages(): except Exception as e: print(f" ⚠️ Error: Privacy Policy - {str(e)}") + db.commit() + print() print("=" * 80) print("✅ Platform pages creation completed successfully!") diff --git a/scripts/init_log_settings.py b/scripts/init_log_settings.py index 1a17ee86..8edfbb1e 100644 --- a/scripts/init_log_settings.py +++ b/scripts/init_log_settings.py @@ -5,8 +5,24 @@ Initialize default log settings in database. Run this script to create default logging configuration settings. """ -# Import all models to avoid SQLAlchemy relationship issues -import models # noqa: F401 +# Register all models with SQLAlchemy so string-based relationships resolve +for _mod in [ + "app.modules.billing.models", + "app.modules.inventory.models", + "app.modules.cart.models", + "app.modules.messaging.models", + "app.modules.loyalty.models", + "app.modules.catalog.models", + "app.modules.customers.models", + "app.modules.orders.models", + "app.modules.marketplace.models", + "app.modules.cms.models", +]: + try: + __import__(_mod) + except ImportError: + pass + from app.core.database import SessionLocal from app.modules.tenancy.models import AdminSetting diff --git a/scripts/init_production.py b/scripts/init_production.py index 32677388..2d1b6379 100644 --- a/scripts/init_production.py +++ b/scripts/init_production.py @@ -53,6 +53,7 @@ for _mod in [ "app.modules.customers.models", "app.modules.orders.models", "app.modules.marketplace.models", + "app.modules.cms.models", ]: try: __import__(_mod) diff --git a/scripts/seed_email_templates.py b/scripts/seed_email_templates.py index c0e9e451..ad1f6306 100644 --- a/scripts/seed_email_templates.py +++ b/scripts/seed_email_templates.py @@ -12,6 +12,24 @@ from pathlib import Path # Add project root to path sys.path.insert(0, str(Path(__file__).parent.parent)) +# Register all models with SQLAlchemy so string-based relationships resolve +for _mod in [ + "app.modules.billing.models", + "app.modules.inventory.models", + "app.modules.cart.models", + "app.modules.messaging.models", + "app.modules.loyalty.models", + "app.modules.catalog.models", + "app.modules.customers.models", + "app.modules.orders.models", + "app.modules.marketplace.models", + "app.modules.cms.models", +]: + try: + __import__(_mod) + except ImportError: + pass + from app.core.database import get_db from app.modules.messaging.models import EmailCategory, EmailTemplate