Spaces:
Running
Running
Foreign Key Fixes Summary
Issue Resolved
Error: Foreign key associated with column 'scm_trade_shipment.order_id' could not find table 'scm_po' with which to generate a foreign key to target column 'po_id'
Root Cause:
- Multiple
Baseinstances across different model files - Foreign key references not including schema prefixes
- Tables in different schemas causing reference issues
Solutions Applied
1. Created Shared Database Base
File: app/core/database.py
from sqlalchemy.orm import declarative_base
# Shared Base for all SQLAlchemy models
Base = declarative_base()
2. Updated All Models to Use Shared Base
Before:
# Each model file had its own Base
from sqlalchemy.orm import declarative_base
Base = declarative_base()
After:
# All models now use shared Base
from app.core.database import Base
Files Updated:
app/catalogues/models/model.pyapp/inventory/stock/models/model.pyapp/purchases/orders/models/model.pyapp/purchases/receipts/models/model.pyapp/trade_sales/models/model.py
3. Fixed Foreign Key Schema References
Before:
ForeignKey('scm_po.po_id')
ForeignKey('scm_grn.grn_id')
After:
ForeignKey('trans.scm_po.po_id')
ForeignKey('trans.scm_grn.grn_id')
Foreign Keys Fixed:
app/trade_sales/models/model.py:
order_id:'scm_po.po_id'β'trans.scm_po.po_id'shipment_id:'scm_trade_shipment.shipment_id'β'trans.scm_trade_shipment.shipment_id'po_item_id:'scm_po_item.po_item_id'β'trans.scm_po_item.po_item_id'
app/inventory/stock/models/model.py:
ledger_id:'scm_stock_ledger.ledger_id'β'trans.scm_stock_ledger.ledger_id'adjustment_id:'scm_stock_adjustment.adjustment_id'β'trans.scm_stock_adjustment.adjustment_id'
app/purchases/orders/models/model.py:
po_id:'scm_po.po_id'β'trans.scm_po.po_id'
app/purchases/receipts/models/model.py:
po_id:'scm_po.po_id'β'trans.scm_po.po_id'grn_id:'scm_grn.grn_id'β'trans.scm_grn.grn_id'po_item_id:'scm_po_item.po_item_id'β'trans.scm_po_item.po_item_id'grn_item_id:'scm_grn_item.grn_item_id'β'trans.scm_grn_item.grn_item_id'
4. Updated Table Creation Logic
File: app/sql.py
Before:
from app.purchases.orders.models.model import Base
After:
from app.core.database import Base
# Import all models to ensure they're registered with Base
from app.catalogues.models.model import CatalogueRef
from app.inventory.stock.models.model import ScmStock, ScmStockLedger, ScmStockAdjustment, ScmStockTake
from app.purchases.orders.models.model import ScmPo, ScmPoItem, ScmPoStatusLog
from app.purchases.receipts.models.model import ScmGrn, ScmGrnItem, ScmGrnIssue
from app.trade_sales.models.model import ScmTradeShipment, ScmTradeShipmentItem
Database Configuration
Current Setup:
- Database:
cuatrolabs - Schema:
trans(for all SCM tables) - Shared Base: All models use
app.core.database.Base
Tables in trans Schema:
catalogue_ref- Catalogue reference with pricing_levelsscm_stock- Stock snapshotsscm_stock_ledger- Stock movement ledgerscm_stock_adjustment- Stock adjustmentsscm_stock_take- Physical stock auditsscm_po- Purchase ordersscm_po_item- Purchase order itemsscm_po_status_log- PO status changesscm_grn- Goods receipt notesscm_grn_item- GRN itemsscm_grn_issue- GRN issues/discrepanciesscm_trade_shipment- Trade shipmentsscm_trade_shipment_item- Trade shipment items
Verification Results
β All Tests Passed:
- Model Import: All models import successfully with shared Base
- Foreign Keys: All foreign key relationships resolve correctly
- Table Creation:
create_tables()executes without errors - Application Startup: FastAPI app starts without foreign key errors
- Schema Consistency: All tables use
transschema consistently
β Foreign Key Relationships Working:
- Trade shipments β Purchase orders
- Trade shipment items β PO items
- GRN items β Purchase orders
- Stock adjustments β Stock ledger
- All cross-table relationships functional
Benefits Achieved
- Eliminated Foreign Key Errors: All table relationships work correctly
- Schema Consistency: All SCM tables in
transschema - Maintainable Code: Single shared Base for all models
- Database Integrity: Proper foreign key constraints enforced
- Application Stability: No startup errors related to table relationships
Future Maintenance
When Adding New Models:
- Always import from
app.core.database import Base - Include schema in
__table_args__ = {'schema': 'trans'} - Use full schema.table format in foreign keys:
'trans.table_name.column' - Add model imports to
app/sql.pycreate_tables function
Schema Migration Best Practices:
- Use consistent schema across all related tables
- Always specify schema explicitly in models
- Include schema in foreign key references
- Test foreign key relationships after schema changes