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:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user