From 204218c4e751c2c61bd68d76e2161b3aebe9c92e Mon Sep 17 00:00:00 2001 From: dwindown Date: Mon, 22 Dec 2025 21:57:00 +0700 Subject: [PATCH] Fix database constraint error in template seeding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace insert() with upsert() to handle existing templates - Add onConflict: 'key' to update duplicates instead of failing - This resolves "duplicate key value violates unique constraint" error - Templates will now be properly updated/seeded on first visit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/components/admin/settings/NotifikasiTab.tsx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/components/admin/settings/NotifikasiTab.tsx b/src/components/admin/settings/NotifikasiTab.tsx index 7a8a81b..e18d923 100644 --- a/src/components/admin/settings/NotifikasiTab.tsx +++ b/src/components/admin/settings/NotifikasiTab.tsx @@ -422,7 +422,7 @@ export function NotifikasiTab() { const seedTemplates = async () => { try { console.log('Seeding default templates...'); - const toInsert = DEFAULT_TEMPLATES.map(t => ({ + const toUpsert = DEFAULT_TEMPLATES.map(t => ({ key: t.key, name: t.name, is_active: false, @@ -431,8 +431,14 @@ export function NotifikasiTab() { webhook_url: '', })); - console.log('Inserting templates:', toInsert.length); - const { data, error } = await supabase.from('notification_templates').insert(toInsert).select(); + console.log('Upserting templates:', toUpsert.length); + const { data, error } = await supabase + .from('notification_templates') + .upsert(toUpsert, { + onConflict: 'key', + ignoreDuplicates: false + }) + .select(); if (error) { console.error('Error seeding templates:', error); @@ -444,7 +450,7 @@ export function NotifikasiTab() { return; } - console.log('Templates seeded successfully:', data); + console.log('Templates seeded/updated successfully:', data); if (data) { setTemplates(data); toast({