Files
2026-04-23 21:38:24 -04:00

3.1 KiB

How to Fix Seed Data Duplicate Errors

Problem

You're getting duplicate key errors because seed data was partially inserted, and when you try to seed again, it conflicts with existing data.

Quick Fix (5 minutes)

Step 1: Connect to Database

  1. Open SQL Server Management Studio (SSMS)
  2. Connect to your testing server database
  3. Select the PowderCoatingDb database

Step 2: Clean Up Existing Seed Data

  1. Open the file: cleanup-seed-data.sql
  2. IMPORTANT: On line 9, change @CompanyId to your actual company ID
    DECLARE @CompanyId INT = 1  -- Change to your company ID
    
  3. Execute the entire script
  4. Verify it shows "0 Remaining Records" for all tables

Step 3: Re-seed via Web Interface

  1. Log in to your application as SuperAdmin
  2. Navigate to: Platform Management → Seed Data (or /SeedData)
  3. Click "Seed Company Data" for your company
  4. All data should seed successfully now

Finding Your Company ID

Run this query in SSMS:

SELECT Id, CompanyName, CompanyCode FROM Companies WHERE IsDeleted = 0

What Gets Deleted

The cleanup script removes:

  • ✓ All customers
  • ✓ All inventory items
  • ✓ All equipment
  • ✓ All jobs and quotes
  • ✓ All catalog items
  • ✓ All pricing tiers
  • ✓ Operating costs

What Gets Preserved

  • ✓ Your company record
  • ✓ All user accounts
  • ✓ System roles
  • ✓ You can still log in

Why This Happens

The seed data checks if "any" records exist, but if:

  1. Seeding fails partway through (permission error, network issue, etc.)
  2. Then you try to seed again
  3. It tries to insert the same data again → duplicate key errors

Prevention

After I update the seed service with better validation, it will:

  • Check for specific seed data patterns, not just "any" data
  • Validate company codes before creating SKUs
  • Give clear error messages if data is partially seeded

Alternative: Reset Everything (Nuclear Option)

If you want a completely fresh database:

-- WARNING: Deletes ALL data including users (except SuperAdmin)
USE PowderCoatingDb
GO

-- Keep a list of superadmins
SELECT * INTO #TempAdmins FROM AspNetUsers WHERE Email IN ('superadmin@powdercoating.com', 'admin@powdercoating.com')

-- Drop and recreate (or just delete all data)
DELETE FROM JobPhotos
DELETE FROM JobNotes
DELETE FROM JobItems
DELETE FROM Jobs
DELETE FROM QuoteItems
DELETE FROM Quotes
DELETE FROM InventoryTransactions
DELETE FROM InventoryItems
DELETE FROM Equipment
DELETE FROM MaintenanceRecords
DELETE FROM CatalogItems
DELETE FROM Customers
DELETE FROM PricingTiers
DELETE FROM CompanyOperatingCosts

-- Delete non-superadmin users
DELETE FROM AspNetUserRoles WHERE UserId NOT IN (SELECT Id FROM #TempAdmins)
DELETE FROM AspNetUsers WHERE Id NOT IN (SELECT Id FROM #TempAdmins)

-- Keep only one company
DELETE FROM Companies WHERE CompanyCode != 'DEMO'

DROP TABLE #TempAdmins

-- Now go to the web interface and seed system data, then seed company data

Need Help?

Check the logs at:

  • [App-Folder]\logs\errors-YYYYMMDD.txt for today's errors
  • Look for specific duplicate key errors to know which table is failing