Skip to content

Add foreign key from FormSubmission.template_id to Template.id#195

Open
sonikagutha wants to merge 1 commit intofireform-core:mainfrom
sonikagutha:fix/form-submission-template-fk
Open

Add foreign key from FormSubmission.template_id to Template.id#195
sonikagutha wants to merge 1 commit intofireform-core:mainfrom
sonikagutha:fix/form-submission-template-fk

Conversation

@sonikagutha
Copy link

Summary
Adds a database-level foreign key so FormSubmission.template_id must reference an existing Template.id, enforcing referential integrity and preventing orphan submissions.

Changes
(api/db/models.py) : Declared FormSubmission.template_id with Field(foreign_key="template.id") instead of a plain int.

Motivation
Previously, template_id had no FK constraint. The database could accept submissions pointing to non-existent or deleted templates. This PR ensures every submission references a valid template.

Testing
New databases created via python -m api.db.init_db include the constraint.
Inserting a FormSubmission with an invalid template_id is rejected by the DB.

Notes
Existing databases: Unchanged unless re-initialized. To apply the FK on an existing DB, you would need to recreate the schema or run a migration (e.g. new DB + export/import, or a migration script).
No API or request/response changes; behavior is backward compatible for valid data.

Fixes #194

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.

FormSubmission.template_id has no foreign key — referential integrity not enforced

1 participant