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

  1. Check the wrapped cause for the actual driver error.
  2. Drop/truncate sys_departments (or the DB) and re-run InitDB.
  3. Let AutoMigrate recreate sys_departments with the current schema before seeding.
  4. Verify DB connectivity and write privileges.
  5. 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

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


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