diff --git a/seed/seed.go b/seed/seed.go index bb091657..12298473 100644 --- a/seed/seed.go +++ b/seed/seed.go @@ -243,12 +243,17 @@ func importDataFromJSON(filename string, mod *model.Model, options ImportOption, return nil } - // Extract columns from first record + // Extract columns from first record, but only include columns that exist in model columns := []string{} for key := range records[0] { - columns = append(columns, key) + if _, exists := mod.Columns[key]; exists { + columns = append(columns, key) + } } + // Sort columns for consistent ordering + sortColumns(columns) + // Convert to rows format handler := createJSONImportHandler(mod, columns, options, result) @@ -295,12 +300,17 @@ func importDataFromYao(filename string, mod *model.Model, options ImportOption, return nil } - // Extract columns from first record + // Extract columns from first record, but only include columns that exist in model columns := []string{} for key := range records[0] { - columns = append(columns, key) + if _, exists := mod.Columns[key]; exists { + columns = append(columns, key) + } } + // Sort columns for consistent ordering + sortColumns(columns) + // Convert to rows format handler := createJSONImportHandler(mod, columns, options, result) @@ -551,3 +561,16 @@ func parseJSONField(value interface{}, columnType string) interface{} { return jsonValue } + +// sortColumns sorts column names alphabetically for consistent ordering +func sortColumns(columns []string) { + // Simple bubble sort for small arrays + n := len(columns) + for i := 0; i < n-1; i++ { + for j := 0; j < n-i-1; j++ { + if columns[j] > columns[j+1] { + columns[j], columns[j+1] = columns[j+1], columns[j] + } + } + } +} diff --git a/seed/seed_test.go b/seed/seed_test.go index 33020a34..b80dbfb1 100644 --- a/seed/seed_test.go +++ b/seed/seed_test.go @@ -160,6 +160,16 @@ func TestSeedImportYao(t *testing.T) { assert.NotNil(t, result) resultMap, ok := result.(*ImportResult) assert.True(t, ok, "Result should be ImportResult") + + // Print detailed result for debugging + if resultMap.Total == 0 || resultMap.Success == 0 { + t.Logf("Import result: Total=%d, Success=%d, Failure=%d, Ignore=%d", + resultMap.Total, resultMap.Success, resultMap.Failure, resultMap.Ignore) + if len(resultMap.Errors) > 0 { + t.Logf("Errors: %+v", resultMap.Errors) + } + } + assert.Greater(t, resultMap.Total, 0, "Should import at least 1 record") assert.Greater(t, resultMap.Success, 0, "Should have successful imports")