flipped-aurora/gin-vue-admin · error

未找到 %s 表初始化数据

Error message

未找到 %s 表初始化数据

What it means

Thrown by the sys_dictionary_details initializer when the shared init context does not contain the SysDictionary seed data produced by initDict under its InitializerName key. This is a dependency-ordering guard: dictionary details must attach to dictionaries seeded earlier. The wrapped sentinel is system.ErrMissingDependentContext.

Source

Thrown at server/source/system/dictionary_detail.go:48

	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	return db.Migrator().HasTable(&sysModel.SysDictionaryDetail{})
}

func (i *initDictDetail) InitializerName() string {
	return sysModel.SysDictionaryDetail{}.TableName()
}

func (i *initDictDetail) InitializeData(ctx context.Context) (context.Context, error) {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return ctx, system.ErrMissingDBContext
	}
	dicts, ok := ctx.Value(new(initDict).InitializerName()).([]sysModel.SysDictionary)
	if !ok {
		return ctx, errors.Wrap(system.ErrMissingDependentContext,
			fmt.Sprintf("未找到 %s 表初始化数据", sysModel.SysDictionary{}.TableName()))
	}
	True := true
	dicts[0].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
		{Label: "男", Value: "1", Status: &True, Sort: 1},
		{Label: "女", Value: "2", Status: &True, Sort: 2},
	}

	dicts[1].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
		{Label: "smallint", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
		{Label: "mediumint", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
		{Label: "int", Value: "3", Status: &True, Extend: "mysql", Sort: 3},
		{Label: "bigint", Value: "4", Status: &True, Extend: "mysql", Sort: 4},
		{Label: "int2", Value: "5", Status: &True, Extend: "pgsql", Sort: 5},
		{Label: "int4", Value: "6", Status: &True, Extend: "pgsql", Sort: 6},
		{Label: "int6", Value: "7", Status: &True, Extend: "pgsql", Sort: 7},
		{Label: "int8", Value: "8", Status: &True, Extend: "pgsql", Sort: 8},
	}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Ensure initDict runs (and succeeds) before initDictDetail in the initializer registration/execution order
  2. Pass the same ctx through the whole initializer chain instead of constructing a new one per initializer
  3. Check the return error of the dictionary initializer before calling the detail initializer; fix that first (see sys_dictionaries 表数据初始化失败)
  4. In custom/test harnesses, populate ctx with context.WithValue(ctx, new(initDict).InitializerName(), []sysModel.SysDictionary{...}) before calling

Example fix

// before: detail init on a fresh context missing dictionary data
ctx, _ = initDictDetail{}.InitializeData(context.WithValue(ctx, "db", db))
// after: run dependent initializers in order on the shared context
ctx, err = initDict{}.InitializeData(ctx)
if err != nil { return err }
ctx, err = initDictDetail{}.InitializeData(ctx)
Defensive patterns

Strategy: validation

Validate before calling

dicts, ok := ctx.Value(new(initDict).InitializerName()).([]sysModel.SysDictionary)
if !ok || len(dicts) == 0 {
    return errors.New("run initDict initializer before initDictDetail")
}

Type guard

func dictionariesInCtx(ctx context.Context) ([]sysModel.SysDictionary, bool) {
    dicts, ok := ctx.Value(new(initDict).InitializerName()).([]sysModel.SysDictionary)
    return dicts, ok && len(dicts) > 0
}

Try / catch

ctx, err = initDictDetail{}.InitializeData(ctx)
if errors.Is(errors.Cause(err), system.ErrMissingDependentContext) {
    return fmt.Errorf("初始化顺序错误: %w", err)
}

Prevention

When it happens

Trigger: Calling initDictDetail.InitializeData(ctx) with a ctx that never passed through initDict.InitializeData, or the earlier initializer stored a non-[]sysModel.SysDictionary value / failed and stored nothing under new(initDict).InitializerName().

Common situations: Reordering the initializer registration list so the detail initializer runs before the dictionary initializer; reusing a fresh context.Value("db")-only context for a single initializer in tests; an earlier dictionary init error swallowed so its result was never placed in the context.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/c8b159587bdc5d3a. Report an issue: GitHub.