flipped-aurora/gin-vue-admin · error

sys_positions表数据初始化失败!

Error message

sys_positions表数据初始化失败!

What it means

In the same initDepartment initializer, after seeding departments it seeds sys_positions with '总经理/CEO' and '普通员工/STAFF'. This Create failed and was wrapped with sys_positions' table name. Note the department insert already succeeded, so the DB is left with departments but no positions.

Source

Thrown at server/source/system/department.go:65

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) {
		return false
	}
	return true
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Inspect the wrapped driver error for the root cause.
  2. Truncate sys_positions (or wipe the DB) and re-run InitDB so both department and position seeds complete together.
  3. Ensure AutoMigrate created sys_positions with the current schema.
  4. Grant INSERT privileges on sys_positions to the init user.
  5. Re-run /init/initdb and verify both sys_departments and sys_positions are populated.

Example fix

// before: partial init leaves departments without positions
// after: reset both tables before re-running init
TRUNCATE sys_positions;
TRUNCATE sys_departments;
// then POST /init/initdb
Defensive patterns

Strategy: try-catch

Validate before calling

var n int64
db.Model(&sysModel.SysPosition{}).Count(&n)
if n > 0 { /* positions already seeded - truncate before re-init */ }

Try / catch

if err := initdb(); err != nil {
    if strings.Contains(err.Error(), "sys_positions") {
        log.Printf("position seed failed: %v", errors.Unwrap(err))
    }
    return err
}

Prevention

When it happens

Trigger: INSERT INTO sys_positions fails during InitDB - leftover position rows conflicting on re-init (unique Code), stale table schema, connection drop between the two Create calls, or privilege denial.

Common situations: Re-running init on a DB where positions already exist; upgrades with older sys_positions schema; DB user lacking write access only to this table; transient failure between the two seed inserts.

Related errors


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