flipped-aurora/gin-vue-admin · error
sys_departments表数据初始化失败!
Error message
sys_departments表数据初始化失败!
What it means
The initDepartment initializer seeds sys_departments with the root '总公司' department. The Create failed and was wrapped with the table name. Since departments anchor the data-scope (dept_id) permission engine, failure here aborts init and leaves row-level permissions without a root node.
Source
Thrown at server/source/system/department.go:58
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
m := db.Migrator()
return m.HasTable(&sysModel.SysDepartment{}) && m.HasTable(&sysModel.SysPosition{})
}
func (i *initDepartment) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
enable := true
departments := []sysModel.SysDepartment{
{Name: "总公司", ParentId: 0, Ancestors: "0", Sort: 0, Status: &enable},
}
if err := db.Create(&departments).Error; err != nil {
return ctx, errors.Wrap(err, sysModel.SysDepartment{}.TableName()+"表数据初始化失败!")
}
positions := []sysModel.SysPosition{
{Name: "总经理", Code: "CEO", Sort: 1, Status: &enable},
{Name: "普通员工", Code: "STAFF", Sort: 2, Status: &enable},
}
if err := db.Create(&positions).Error; err != nil {
return ctx, errors.Wrap(err, sysModel.SysPosition{}.TableName()+"表数据初始化失败!")
}
next := context.WithValue(ctx, i.InitializerName(), departments)
return next, nil
}
func (i *initDepartment) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where("name = ?", "总公司").First(&sysModel.SysDepartment{}).Error, gorm.ErrRecordNotFound) {View on GitHub (pinned to 3136500ef3)
Solutions
- Check the wrapped cause for the actual driver error.
- Drop/truncate sys_departments (or the DB) and re-run InitDB.
- Let AutoMigrate recreate sys_departments with the current schema before seeding.
- Verify DB connectivity and write privileges.
- Re-run /init/initdb; note positions seeding follows immediately in the same initializer.
Defensive patterns
Strategy: try-catch
Validate before calling
var n int64
db.Model(&sysModel.SysDepartment{}).Count(&n)
if n > 0 { /* departments already seeded - wipe or skip before init */ } Try / catch
if err := initdb(); err != nil {
if strings.Contains(err.Error(), "sys_departments") {
log.Printf("department seed failed: %v", errors.Unwrap(err))
}
return err
} Prevention
- Initialize against an empty database
- Keep AutoMigrate on so ancestors/status columns exist
- Verify INSERT privileges before init
- Remember positions seed runs right after - fix both on retry
When it happens
Trigger: INSERT INTO sys_departments fails during InitDB - duplicate root row on re-init, stale/missing columns (e.g. ancestors, status), connection failure, or insufficient privileges.
Common situations: Re-running initdb on a partially initialized DB; upgrades over old department schema; DB user without INSERT; using a database where department table creation was skipped.
Related errors
- media.FileUploadAndDownload表数据初始化失败!
- sys_apis表数据初始化失败!
- sys_ignore_apis表数据初始化失败!
- 为超级管理员分配菜单失败
- 为普通用户分配菜单失败
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/8682cb97311675e5.
Report an issue: GitHub.