Skip to Content
Backend ReferenceThe Database

Database Technical Reference

This guide provides a comprehensive breakdown of the PostgreSQL schema used by PlugZero Analytics. It details every table, foreign key relationship, and field type.


🏗️ Organizational Domain (accounts)

This layer manages multi-tenancy, user identity, and team-based access control.

1. User Profile (User)

Extends Django’s AbstractUser. Handles authentication and tier-based segmentation.

FieldTypeKeyDescription
idBigIntPKInternal unique identifier.
emailVarChar(255)UniqueThe primary login identifier.
segmentVarChar(20)-User tier: STUDENT, SMB, ENTERPRISE.
institutionVarChar(255)-University or company name.
is_2fa_enabledBoolean-Security flag for multi-factor status.
totp_secretVarChar(255)-Encrypted seed for TOTP apps.

2. Team Workspace (Team)

The root of data ownership.

FieldTypeKeyDescription
idBigIntPKInternal unique identifier.
nameVarChar(255)-Display name of the team workspace.
owner_idBigIntFKLinks to User table (Owner).
created_atDateTime-Date of team creation.

3. Team Membership (TeamMembership)

Links users to teams with specific permission roles.

FieldTypeKeyDescription
user_idBigIntFKLinks to User.
team_idBigIntFKLinks to Team.
roleVarChar(20)-Role: OWNER, ADMIN, MEMBER, VIEWER.

📂 Ingestion & Project Domain (data_ingestion)

Handles raw research assets, surveys, and collection workflows.

1. Research Project (Project)

A container for all research assets within a team.

FieldTypeKeyDescription
idBigIntPKInternal unique identifier.
team_idBigIntFKLinks to Team. Projects are siloed by team.
created_by_idBigIntFKLinks to User.
nameVarChar(255)-Project title.
descriptionText-Background context for the AI engine.

2. Raw Asset (RawDataFile)

Stores uploaded CSV, Excel, and PDF files.

FieldTypeKeyDescription
project_idBigIntFKLinks to Project.
fileFilePath-S3 path to the original object.
file_typeVarChar(20)-TABULAR or UNSTRUCTURED.
upload_statusVarChar(20)-PENDING, SUCCESS, FAILED.
columns_metadataJSONB-Map of detected column names and types.

3. Survey Engine (Survey)

The configuration for research collection missions.

FieldTypeKeyDescription
uuidUUIDUniquePublic identifier for survey links.
project_idBigIntFKLinks to Project.
titleVarChar(255)-Public survey title.
layout_styleVarChar(10)-CLASSIC (scroll) or FOCUS (one-by-one).
theme_configJSONB-UI colors, fonts, and background images.

🧠 Intelligence & Analytics Domain (analysis, intelligence)

Stores the computed findings and external web data.

1. Analysis Result (AnalysisResult)

The persistent cache for heavy mathematical calculations.

FieldTypeKeyDescription
idBigIntPKInternal unique identifier.
project_idBigIntFKLinks to Project.
calculation_typeVarChar(50)-MEAN, CROSSTAB, SENTIMENT, etc.
parametersJSONB-Variable selections (e.g., Column X vs Column Y).
result_dataJSONB-The final statistical output table.

2. Research Insight (ResearchInsight)

Atomic findings generated by AI or human analysts.

FieldTypeKeyDescription
project_idBigIntFKLinks to Project.
titleVarChar(255)-The “Headline” finding.
ai_generatedBoolean-Flag for human vs AI provenance.
embeddingJSONB-Vector representation for semantic search.

3. SEO Site Audit (SiteAudit)

Head record for technical website scans.

FieldTypeKeyDescription
project_idBigIntFKLinks to Project.
target_urlURL-The base URL being audited.
status_codesJSONB-Breakdown of 200 vs 404 vs 500 counts.
scoreInteger-Calculated health score (0-100).

🕸️ Entity Relationship Overview

The diagram below shows the core “Foreign Key” flow of the system:

1. The Multi-Tenant Anchor

TeamProject (All research is isolated by the team_id foreign key).

2. The Data Sources

ProjectRawDataFile (Many-to-One)
ProjectSurvey (Many-to-One)
ProjectScrapeTarget (Many-to-One)

3. The Computation Flow

ProjectAnalysisResult (One-to-Many cache)
AnalysisResultResearchInsight (Findings are derived from analysis results).

4. The Collaboration Layer

ReportComment (Anchored collaboration on text ranges).


🛠️ Performance & Constraints

  1. Indexing Strategy:
    • Every Foreign Key has a B-Tree index by default (Django convention).
    • Composite indexes are used on (project_id, created_at) for tables with high time-series traffic (e.g., ExperienceEvent).
  2. JSONB Usage: We utilize PostgreSQL JSONB for all flexible metadata. This allows for rapid schema changes without requiring database migrations for every new survey question type.
  3. Soft Deletion: While we use on_delete=models.CASCADE for most child objects, we use logical flags for Users and Teams to prevent accidental mass data loss.

Strict Deployment Rule: Never run ALTER TABLE commands manually in production. All schema changes MUST go through Django Migrations to ensure consistency between the local environment, the build server, and our production RDS instance.


Last updated on