flipped-aurora/gin-vue-admin · error
sys_dictionaries表数据初始化失败!
Error message
sys_dictionaries表数据初始化失败!
What it means
This error is thrown by the sys_dictionaries system-table initializer (initDict.InitializeData) when the bulk GORM insert of the seed dictionary rows (数据库浮点型/字符串/bool类型 etc.) fails. It wraps the underlying *gorm.DB error with pkg/errors.Wrap so the failing table is named. It occurs during system initialization (first boot / RegisterInit).
Source
Thrown at server/source/system/dictionary.go:56
}
func (i *initDict) InitializeData(ctx context.Context) (next context.Context, err error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
True := true
entities := []sysModel.SysDictionary{
{Name: "性别", Type: "gender", Status: &True, Desc: "性别字典"},
{Name: "数据库int类型", Type: "int", Status: &True, Desc: "int类型对应的数据库类型"},
{Name: "数据库时间日期类型", Type: "time.Time", Status: &True, Desc: "数据库时间日期类型"},
{Name: "数据库浮点型", Type: "float64", Status: &True, Desc: "数据库浮点型"},
{Name: "数据库字符串", Type: "string", Status: &True, Desc: "数据库字符串"},
{Name: "数据库bool类型", Type: "bool", Status: &True, Desc: "数据库bool类型"},
}
if err = db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, sysModel.SysDictionary{}.TableName()+"表数据初始化失败!")
}
next = context.WithValue(ctx, i.InitializerName(), entities)
return next, nil
}
func (i *initDict) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where("type = ?", "bool").First(&sysModel.SysDictionary{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
return false
}
return true
}
View on GitHub (pinned to 3136500ef3)
Solutions
- Check the wrapped cause (errors.Cause / %v of err) to see the raw GORM/SQL error and fix the root DB issue
- Ensure the table exists first: run the AutoMigrate step for SysDictionary before InitializeData
- Drop and recreate the database (or TRUNCATE sys_dictionaries) so the seed insert starts from a clean state
- Verify DB connectivity/credentials in config (host, port, user, password, dbname) and that the server is reachable
- Confirm the dialect-specific model columns are compatible with your configured DB (e.g. pgsql vs mysql)
Example fix
// before: init runs before migration, table missing
count, _ := db.Count(...) // assume exists
initDict{}.InitializeData(ctx)
// after: migrate then init, and only if not already inserted
if !db.Migrator().HasTable(&sysModel.SysDictionary{}) {
_ = db.AutoMigrate(&sysModel.SysDictionary{})
}
if !(initDict{}).DataInserted(ctx) {
ctx, err = initDict{}.InitializeData(ctx)
} Defensive patterns
Strategy: validation
Validate before calling
if !db.Migrator().HasTable(&sysModel.SysDictionary{}) {
if err := db.AutoMigrate(&sysModel.SysDictionary{}); err != nil { return err }
}
var count int64
if err := db.Model(&sysModel.SysDictionary{}).Count(&count).Error; err != nil { return err }
if count > 0 { return nil } // already seeded, skip Type guard
func hasDB(ctx context.Context) (*gorm.DB, bool) {
db, ok := ctx.Value("db").(*gorm.DB)
return db, ok && db != nil
} Try / catch
ctx, err := initDict{}.InitializeData(ctx)
if err != nil {
log.Printf("dict seed failed: %v (cause: %v)", err, errors.Cause(err))
return fmt.Errorf("初始化字典失败: %w", err)
} Prevention
- Always run AutoMigrate for all seed models before any initializer
- Call DataInserted(ctx) before seeding to make init idempotent
- Initialize against a fresh/empty database and run init exactly once
- Check DB connectivity and credentials at startup before the init chain
- Log errors.Cause(err) to expose the raw GORM/SQL driver error
When it happens
Trigger: Calling InitializeData with the *gorm.DB stored in ctx under key "db" when db.Create(&entities) returns a non-nil error — e.g. the sys_dictionaries table does not exist (AutoMigrate not run), a duplicate primary key/unique conflict from partial prior seeding, or a DB connection failure mid-insert.
Common situations: Running the initializer against a database where the table was partially created by an earlier failed init; connecting to a DB with wrong credentials or a dropped connection; switching DB dialects (mysql/pgsql/sqlite) where a column type in the seed model is unsupported; running init twice without truncation causing duplicate-key errors.
Related errors
- media.FileUploadAndDownload表数据初始化失败!
- sys_apis表数据初始化失败!
- sys_ignore_apis表数据初始化失败!
- %s表数据初始化失败!
- sys_positions表数据初始化失败!
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/58b90f265df3bd0e.
Report an issue: GitHub.