Fix database constraint error in template seeding

- 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 <noreply@anthropic.com>
This commit is contained in:
dwindown
2025-12-22 21:57:00 +07:00
parent 78e7b946ac
commit 204218c4e7

View File

@@ -422,7 +422,7 @@ export function NotifikasiTab() {
const seedTemplates = async () => { const seedTemplates = async () => {
try { try {
console.log('Seeding default templates...'); console.log('Seeding default templates...');
const toInsert = DEFAULT_TEMPLATES.map(t => ({ const toUpsert = DEFAULT_TEMPLATES.map(t => ({
key: t.key, key: t.key,
name: t.name, name: t.name,
is_active: false, is_active: false,
@@ -431,8 +431,14 @@ export function NotifikasiTab() {
webhook_url: '', webhook_url: '',
})); }));
console.log('Inserting templates:', toInsert.length); console.log('Upserting templates:', toUpsert.length);
const { data, error } = await supabase.from('notification_templates').insert(toInsert).select(); const { data, error } = await supabase
.from('notification_templates')
.upsert(toUpsert, {
onConflict: 'key',
ignoreDuplicates: false
})
.select();
if (error) { if (error) {
console.error('Error seeding templates:', error); console.error('Error seeding templates:', error);
@@ -444,7 +450,7 @@ export function NotifikasiTab() {
return; return;
} }
console.log('Templates seeded successfully:', data); console.log('Templates seeded/updated successfully:', data);
if (data) { if (data) {
setTemplates(data); setTemplates(data);
toast({ toast({