fix(scripts): fix model registration and missing platform_id in init scripts
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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"],
|
||||
|
||||
Reference in New Issue
Block a user