OptimaAI Logo

Database

PostgreSQL schema and SQLAlchemy models

Database

OptimaAi uses PostgreSQL with SQLAlchemy as the ORM. Tables are created automatically on startup via init_db().

Connection

The connection string is read from the DATABASE_URL environment variable:

DATABASE_URL=postgresql://optimaai:optimaai123@localhost:5432/optimaai_db

Default: postgresql://optimaai:optimaai123@localhost:5432/optimaai_db

Tables

kpi_snapshots

Stores a timestamped snapshot every time KPIs are loaded from ML artefacts.

ColumnTypeDescription
idInteger PKAuto-increment primary key
created_atDateTimeSnapshot creation timestamp
model_versionString(50)Optional model version tag
revenue_mapeFloatRevenue model MAPE %
revenue_r2FloatRevenue model R²
revenue_maeFloatRevenue model MAE
forecast_biasFloatSigned forecast bias
churn_roc_aucFloatChurn model ROC-AUC
churn_f1FloatChurn model F1 score
churn_rate_pctFloatDataset churn rate %
growth_mapeFloatGrowth model MAPE %
growth_r2FloatGrowth model R²
prophet_mapeFloatProphet MAPE %
revenue_forecast_12JSON12-period revenue forecast array
growth_forecast_12JSON12-month growth forecast array

recommendations

Stores every LLM-generated recommendation.

ColumnTypeDescription
idInteger PKAuto-increment primary key
created_atDateTimeGeneration timestamp
roleString(50)User role (e.g. Sales Manager)
recommendationTextFull recommendation text
model_usedString(100)LLM model identifier
kpi_snapshot_idIntegerFK → kpi_snapshots.id

bmc_results

Stores every generated Business Model Canvas.

ColumnTypeDescription
idInteger PKAuto-increment primary key
created_atDateTimeGeneration timestamp
platform_nameString(100)Platform label
bmc_textTextRaw markdown of the full BMC
bmc_blocksJSONParsed dict of 9 canvas sections
model_usedString(100)LLM model identifier
kpi_snapshot_idIntegerFK → kpi_snapshots.id

uploads (dynamic)

Created by datasets_routes.py at runtime. Tracks every file uploaded via the Datasets page.

ColumnTypeDescription
idInteger PKAuto-increment
created_atDateTimeUpload timestamp
filenameStringOriginal filename
categoryStringUser-assigned category tag
user_idIntegerUploader ID
row_countIntegerRows after preprocessing
quality_scoreFloatData quality score (0–100)
cleaned_tableStringName of the cleaned data table
kb_ingestedBooleanWhether it was embedded into ChromaDB

Session Management

A SQLAlchemy session is injected into every route via FastAPI's dependency injection:

from app.database import get_db
from sqlalchemy.orm import Session
from fastapi import Depends
 
@router.get("/example")
def my_route(db: Session = Depends(get_db)):
    # db is automatically closed after the request
    ...

Manual Table Creation

from app.database import init_db
init_db()  # Creates all tables if they don't exist

On this page