fix: Use upsert for profile updates to handle missing rows

Changes:
1. Created migration 003_fix_profile_creation.sql:
   - Added INSERT policy for profiles table
   - Made username nullable with default value
   - Created upsert_profile function for safe upserts

2. Updated ProfileSettings.tsx:
   - Changed from .update() to .upsert()
   - Added id field to upsert data
   - Added loadProfile() call after save to refresh state

This fixes the issue where users couldn't update their profile
because no profile row existed in the database.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-29 04:45:23 +03:00
parent 52e35e5179
commit c9939ee656
2 changed files with 96 additions and 7 deletions
+14 -7
View File
@@ -80,8 +80,9 @@ export default function ProfileSettings() {
const updateProfile = async () => {
setLoading(true);
try {
const updateData = {
username: profile.username,
const profileData = {
id: user?.id,
username: profile.username || '',
full_name: profile.full_name,
bio: profile.bio,
phone_number: profile.phone_number,
@@ -92,17 +93,20 @@ export default function ProfileSettings() {
updated_at: new Date().toISOString()
};
console.log('💾 SAVING PROFILE DATA:', updateData);
console.log('💾 UPSERTING PROFILE DATA:', profileData);
console.log('👤 User ID:', user?.id);
// Use upsert to create row if it doesn't exist, or update if it does
const { data, error } = await supabase
.from('profiles')
.update(updateData)
.eq('id', user?.id)
.upsert(profileData, {
onConflict: 'id',
ignoreDuplicates: false
})
.select();
console.log('✅ Save response data:', data);
console.log('❌ Save error:', error);
console.log('✅ Upsert response data:', data);
console.log('❌ Upsert error:', error);
if (error) throw error;
@@ -110,6 +114,9 @@ export default function ProfileSettings() {
title: 'Success',
description: 'Profile updated successfully',
});
// Reload profile to ensure state is in sync
await loadProfile();
} catch (error) {
console.error('❌ Profile update failed:', error);
toast({