Skip to content

Comments

Fix #134: Add validation for duplicate profile names#175

Open
Vaibhavee89 wants to merge 1 commit intojamiepine:mainfrom
Vaibhavee89:fix/profile-duplicate-name-validation
Open

Fix #134: Add validation for duplicate profile names#175
Vaibhavee89 wants to merge 1 commit intojamiepine:mainfrom
Vaibhavee89:fix/profile-duplicate-name-validation

Conversation

@Vaibhavee89
Copy link

Summary

This PR fixes issue #134 where users encountered database errors when trying to save profiles with duplicate names.

Changes

Backend Validation (backend/profiles.py)

  • ✅ Added validation in create_profile() to check for existing profile names before database insert
  • ✅ Added validation in update_profile() to prevent renaming to duplicate names (while allowing profiles to keep their own name)
  • ✅ Raises user-friendly ValueError with clear message: "A profile with the name 'X' already exists. Please choose a different name."

API Error Handling (backend/main.py)

  • ✅ Improved error handling in create_profile endpoint with explicit ValueError catching
  • ✅ Added try-catch block in update_profile endpoint (previously had no error handling)
  • ✅ Both endpoints now return HTTP 400 with clear, actionable error messages

Testing (backend/tests/test_profile_duplicate_names.py)

  • ✅ Comprehensive test suite covering all scenarios:
    • Creating profiles with duplicate names
    • Creating profiles with unique names
    • Updating profiles to duplicate names
    • Updating profiles while keeping the same name
    • Updating profiles to new unique names
    • Case sensitivity verification

Documentation

  • ✅ Updated CHANGELOG.md with fix details

Before This Fix

  • ❌ Error message: UNIQUE constraint failed: profiles.name
  • ❌ Confusing for users
  • ❌ No guidance on resolution

After This Fix

  • ✅ Error message: A profile with the name 'Test Profile' already exists. Please choose a different name.
  • ✅ Clear and actionable
  • ✅ Guides users to the solution

Testing

Manual Testing

Verified that:

  1. Creating a profile with a duplicate name shows the improved error message
  2. Updating a profile to a duplicate name shows the improved error message
  3. Updating a profile while keeping the same name works correctly
  4. Creating/updating profiles with unique names works as expected

Automated Tests

Run the test suite:

cd backend
pip install pytest pytest-asyncio
pytest tests/test_profile_duplicate_names.py -v

Checklist

  • Code follows style guidelines (PEP 8 for Python)
  • Documentation updated (CHANGELOG.md)
  • Changes tested manually and with automated tests
  • No breaking changes
  • User-friendly error messages implemented
  • Comprehensive test coverage added

Related Issues

Closes #134

Screenshots

The original issue included a screenshot of the database error. After this fix, users will see a clear message instead.

- Add validation in create_profile() to check for existing names before insert
- Add validation in update_profile() to prevent renaming to duplicate names
- Improve error handling in API endpoints with user-friendly messages
- Add comprehensive test suite for duplicate name validation
- Update CHANGELOG.md with fix details

This fix prevents database constraint violations and provides clear
error messages when users attempt to create or update profiles with
names that already exist in the database.
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.

can't save Profile

1 participant