fix: auto-create database tables on application startup#420
Open
Arijit429 wants to merge 8 commits intofireform-core:mainfrom
Open
fix: auto-create database tables on application startup#420Arijit429 wants to merge 8 commits intofireform-core:mainfrom
Arijit429 wants to merge 8 commits intofireform-core:mainfrom
Conversation
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. |
95bedce to
a34973c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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:
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():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:
This prevented:
✅ Solution
By initializing the schema during FastAPI startup, the application now automatically creates all required tables before any route is accessed.
Updated startup flow
This makes the local development and testing workflow significantly smoother.
💡 Code-Level Explanation
The key logic added is:
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
/templates/create/forms/fill🎯 Impact