Skip to content

fix: auto-create database tables on application startup#420

Open
Arijit429 wants to merge 8 commits intofireform-core:mainfrom
Arijit429:startup-db-init-fix
Open

fix: auto-create database tables on application startup#420
Arijit429 wants to merge 8 commits intofireform-core:mainfrom
Arijit429:startup-db-init-fix

Conversation

@Arijit429
Copy link
Copy Markdown

@Arijit429 Arijit429 commented Apr 11, 2026

Closes #293

🚀 Summary

This PR fixes application startup failures caused by missing database tables in fresh local environments and improves the developer setup workflow by ensuring schema initialization happens automatically during application startup.


✨ What Changed

1) Automatic database table creation on startup

Added automatic schema initialization in api/main.py:

from sqlmodel import SQLModel
from api.db.database import engine
from api.db.models import *

SQLModel.metadata.create_all(engine)

This ensures that all registered SQLModel tables are created whenever the application starts.


2) Correct database import path

Updated the database engine import path to use the project’s existing database module structure:

from api.db.database import engine

This fixes the previous import issue caused by an incorrect module path.


3) Model registration before schema creation

Imported all database models before calling create_all():

from api.db.models import *

This is important because SQLModel must first load all model classes so their metadata can be registered before table creation.


🐛 Problem

Previously, running the application with a fresh SQLite database or after deleting the existing DB file caused runtime failures such as:

sqlalchemy.exc.OperationalError: no such table: template

This prevented:

  • first-time local setup
  • clean environment testing
  • schema recreation after database reset

✅ Solution

By initializing the schema during FastAPI startup, the application now automatically creates all required tables before any route is accessed.

Updated startup flow

FastAPI Startup
   ↓
Import DB Models
   ↓
Register SQLModel Metadata
   ↓
Auto Create Tables
   ↓
Include Routes
   ↓
Application Ready

This makes the local development and testing workflow significantly smoother.


💡 Code-Level Explanation

The key logic added is:

SQLModel.metadata.create_all(engine)

This line checks all registered SQLModel models and creates any missing tables in the connected SQLite database.

It avoids manual DB initialization steps and ensures schema consistency during development.


🧪 Testing Performed

  • deleted existing local SQLite database
  • restarted FastAPI server
  • verified automatic table creation
  • successfully tested /templates/create
  • successfully tested /forms/fill
  • verified successful end-to-end report generation workflow

🎯 Impact

  • improves first-time developer onboarding
  • removes manual database initialization dependency
  • reduces setup-related runtime errors
  • improves local testing reliability
  • strengthens development workflow consistency

@Arijit429
Copy link
Copy Markdown
Author

Hi maintainers, tested this locally with a fresh DB setup and verified the full route flow—happy to make any refinements based on your feedback.

@Arijit429 Arijit429 force-pushed the startup-db-init-fix branch from 95bedce to a34973c Compare April 19, 2026 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"[BUG]: 500 Internal Server Error on Backend for /templates/create and /forms/fill

1 participant