2.9 KiB
2.9 KiB
SQL WHERE Clause Fixes
Problem
When activating the MOTM vote, the application was throwing an error: "Database not initialized. Please go to Database Setup to initialize the database."
Root Cause
Multiple SQL queries on the admin_settings table were missing WHERE userid = 'admin' clauses, causing:
- UPDATE queries to update ALL rows instead of just the admin row
- SELECT queries to return unexpected results
- The application to think the database wasn't initialized
Fixes Applied
1. UPDATE Queries Fixed
Line 599 - Main settings update:
-- BEFORE:
UPDATE admin_settings SET next_date = :next_date, next_club = :next_club, ...
-- AFTER:
UPDATE admin_settings SET next_date = :next_date, next_club = :next_club, ... WHERE userid = 'admin'
Line 613 - Opponent logo update:
-- BEFORE:
UPDATE admin_settings SET oppo_logo = :logo_url
-- AFTER:
UPDATE admin_settings SET oppo_logo = :logo_url WHERE userid = 'admin'
2. SELECT Queries Fixed
Line 256 - MOTM vote page:
-- BEFORE:
SELECT next_club, next_team, next_date, ... FROM admin_settings
-- AFTER:
SELECT next_club, next_team, next_date, ... FROM admin_settings WHERE userid = 'admin'
Line 348 - Match comments page:
-- BEFORE:
SELECT next_club, next_team, next_date, oppo_logo, hkfc_logo FROM admin_settings
-- AFTER:
SELECT next_club, next_team, next_date, oppo_logo, hkfc_logo FROM admin_settings WHERE userid = 'admin'
Line 683 - Admin settings page:
-- BEFORE:
SELECT next_club, oppo_logo FROM admin_settings
-- AFTER:
SELECT next_club, oppo_logo FROM admin_settings WHERE userid = 'admin'
Why This Matters
Without the WHERE clause:
- UPDATE queries would modify all rows in the table (even if there's only one row, this is bad practice)
- SELECT queries might return multiple rows when only one is expected
- The application logic assumes only one admin settings row exists
Testing
After these fixes:
- ✅ The MOTM admin page should save settings correctly
- ✅ Activating the MOTM vote should work without errors
- ✅ The voting page should load correctly
- ✅ All admin settings queries will target only the admin row
Deployment
To apply these fixes to production:
- Commit the changes to your repository
- Rebuild and redeploy the Docker image
- Restart the application pods
# Build new image
docker build -t your-registry/motm-app:latest .
# Push to registry
docker push your-registry/motm-app:latest
# Deploy to Kubernetes
helm upgrade motm-app ./helm-chart/motm-app --namespace motm-app
Related Issues
These fixes are related to:
- The voting deadline feature implementation
- The table name migration from
motmadminsettingstoadmin_settings - The column name migration to snake_case
Summary
All SQL queries on the admin_settings table now properly filter by userid = 'admin', ensuring:
- Data integrity
- Predictable query results
- Proper application functionality