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:
2026-02-09 21:16:42 +01:00
parent 68493dc6cb
commit d201221fb1
5 changed files with 104 additions and 2 deletions

View File

@@ -36,8 +36,27 @@ sys.path.insert(0, str(project_root))
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.orm import Session 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.core.database import SessionLocal
from app.modules.cms.models import ContentPage from app.modules.cms.models import ContentPage
from app.modules.tenancy.models import Platform
# ============================================================================ # ============================================================================
# DEFAULT PAGE CONTENT # DEFAULT PAGE CONTENT
@@ -470,6 +489,15 @@ def create_default_pages(db: Session) -> None:
print("Creating Default Platform Content Pages (CMS)") print("Creating Default Platform Content Pages (CMS)")
print("=" * 70 + "\n") 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 created_count = 0
skipped_count = 0 skipped_count = 0
@@ -490,6 +518,7 @@ def create_default_pages(db: Session) -> None:
# Create new platform default page # Create new platform default page
page = ContentPage( page = ContentPage(
platform_id=platform_id,
store_id=None, # Platform default store_id=None, # Platform default
slug=page_data["slug"], slug=page_data["slug"],
title=page_data["title"], title=page_data["title"],

View File

@@ -23,8 +23,29 @@ from pathlib import Path
project_root = Path(__file__).resolve().parent.parent project_root = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(project_root)) 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.core.database import SessionLocal
from app.modules.cms.services import content_page_service from app.modules.cms.services import content_page_service
from app.modules.tenancy.models import Platform
def create_platform_pages(): def create_platform_pages():
@@ -40,6 +61,15 @@ def create_platform_pages():
# Import ContentPage for checking existing pages # Import ContentPage for checking existing pages
from app.modules.cms.models import ContentPage 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 # 1. PLATFORM HOMEPAGE
# ======================================================================== # ========================================================================
@@ -59,6 +89,7 @@ def create_platform_pages():
try: try:
homepage = content_page_service.create_page( homepage = content_page_service.create_page(
db, db,
platform_id=platform_id,
slug="platform_homepage", slug="platform_homepage",
title="Welcome to Our Multi-Store Marketplace", title="Welcome to Our Multi-Store Marketplace",
content=""" content="""
@@ -95,6 +126,7 @@ def create_platform_pages():
try: try:
about = content_page_service.create_page( about = content_page_service.create_page(
db, db,
platform_id=platform_id,
slug="about", slug="about",
title="About Us", title="About Us",
content=""" content="""
@@ -151,6 +183,7 @@ def create_platform_pages():
try: try:
faq = content_page_service.create_page( faq = content_page_service.create_page(
db, db,
platform_id=platform_id,
slug="faq", slug="faq",
title="Frequently Asked Questions", title="Frequently Asked Questions",
content=""" content="""
@@ -236,6 +269,7 @@ def create_platform_pages():
try: try:
contact = content_page_service.create_page( contact = content_page_service.create_page(
db, db,
platform_id=platform_id,
slug="contact", slug="contact",
title="Contact Us", title="Contact Us",
content=""" content="""
@@ -305,6 +339,7 @@ def create_platform_pages():
try: try:
terms = content_page_service.create_page( terms = content_page_service.create_page(
db, db,
platform_id=platform_id,
slug="terms", slug="terms",
title="Terms of Service", title="Terms of Service",
content=""" content="""
@@ -397,6 +432,7 @@ def create_platform_pages():
try: try:
privacy = content_page_service.create_page( privacy = content_page_service.create_page(
db, db,
platform_id=platform_id,
slug="privacy", slug="privacy",
title="Privacy Policy", title="Privacy Policy",
content=""" content="""
@@ -477,6 +513,8 @@ def create_platform_pages():
except Exception as e: except Exception as e:
print(f" ⚠️ Error: Privacy Policy - {str(e)}") print(f" ⚠️ Error: Privacy Policy - {str(e)}")
db.commit()
print() print()
print("=" * 80) print("=" * 80)
print("✅ Platform pages creation completed successfully!") print("✅ Platform pages creation completed successfully!")

View File

@@ -5,8 +5,24 @@ Initialize default log settings in database.
Run this script to create default logging configuration settings. Run this script to create default logging configuration settings.
""" """
# Import all models to avoid SQLAlchemy relationship issues # Register all models with SQLAlchemy so string-based relationships resolve
import models # noqa: F401 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.core.database import SessionLocal
from app.modules.tenancy.models import AdminSetting from app.modules.tenancy.models import AdminSetting

View File

@@ -53,6 +53,7 @@ for _mod in [
"app.modules.customers.models", "app.modules.customers.models",
"app.modules.orders.models", "app.modules.orders.models",
"app.modules.marketplace.models", "app.modules.marketplace.models",
"app.modules.cms.models",
]: ]:
try: try:
__import__(_mod) __import__(_mod)

View File

@@ -12,6 +12,24 @@ from pathlib import Path
# Add project root to path # Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent)) 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.core.database import get_db
from app.modules.messaging.models import EmailCategory, EmailTemplate from app.modules.messaging.models import EmailCategory, EmailTemplate