fix/duplicates in seeding script
Merge Request
Overview
This MR fixes a database seeding issue caused by duplicate badge entries in the seed data. The issue resulted in a PostgreSQL UniqueViolation error when running the database seed script.
The change removes the duplicate Active Volunteer badge entry from the badge seed list to ensure successful database initialization.
What does this MR do and why?
The seed script failed during execution because the badges table enforces a unique constraint on the name column (badges_name_key).
While seeding data, the script attempted to insert the badge Active Volunteer twice, causing PostgreSQL to reject the second insert.
Root Cause
Duplicate badge entry in the seed data:
{
"name": "Active Volunteer",
"description": "Awarded for 10 camp visits",
}
appeared multiple times in the badge seed list.
Fix Applied
- Removed the duplicate
Active Volunteerbadge entry from the seed data. - Ensured all badge names remain unique.
- Verified successful database seeding after the fix.
Changes Made
- Removed duplicate
Active Volunteerbadge seed entry. - Updated seed data consistency.
- Verified database seeding executes successfully.
Technical Details
Root Cause
The PostgreSQL database contains a unique constraint on the badges.name field:
CONSTRAINT badges_name_key UNIQUE(name)
During db.flush() in seed_badges(), SQLAlchemy attempted to insert duplicate rows with the same badge name.
Resolution
The duplicate badge object was removed from the badge seed list to maintain data integrity and satisfy the database constraint.
Type of Change
-
🐛 Bug fix (non-breaking change that fixes an issue) -
✨ New feature -
💥 Breaking change -
📝 Documentation update -
♻ ️ Refactor -
⚡ Performance improvement -
🧪 Test update -
🔧 Configuration change -
🚨 Security fix -
🗑 ️ Deprecation
Related Issues / References
- Fixes database seeding failure caused by duplicate badge entries.
How to Validate Locally
Previous Behaviour
Running the seed script caused the application to fail with a PostgreSQL UniqueViolation error.
Steps to Validate
python scripts/seed_database.py
Expected Result
- Database seeding completes successfully.
- No duplicate key or integrity errors occur.
- All badge records are inserted correctly.
Testing Done
-
Database seeding tested successfully
| Scenario | Expected Result | Status |
|---|---|---|
| Run database seeding | Seeding completes successfully | |
| Verify unique badge entries | No duplicate badge names exist | |
| Application startup after seeding | Application starts normally |
Test Commands Run
python scripts/seed_database.py
pytest
Code Quality Checklist
-
Code follows project conventions -
No duplicate seed entries remain -
No unused imports or variables -
Data integrity constraints maintained -
SQLAlchemy ORM used properly -
Error resolved successfully
Known Limitations / Technical Debt
- Seed scripts still rely on manually maintained static data.
- Future improvements can include idempotent seeding using
ON CONFLICT DO NOTHING.
Additional Notes
This fix resolves the duplicate badge insertion issue without requiring schema changes.