flipped-aurora/gin-vue-admin · critical

Casbin 表 ( ) 数据初始化失败!

Error message

Casbin 表 (%s) 数据初始化失败!

What it means

The initCasbin initializer seeds the Casbin policy table (casbin_rule) with the default p rules per role. The bulk Create failed and was wrapped with the table name via i.InitializerName(). Without these policies, Casbin authorization denies every seeded route.

Solutions

  1. Read the wrapped driver error (duplicate key, unknown column, access denied).
  2. Truncate/drop the Casbin rule table (or whole DB) and re-run InitDB for a clean seed.
  3. Confirm the Casbin adapter table name and schema match the configured model (i.InitializerName()).
  4. Verify DB privileges and connection stability for the bulk insert.
  5. Re-run /init/initdb and confirm policies exist for roles 888/9528/8881.
Defensive patterns

Strategy: try-catch

Validate before calling

// check casbin rule table state before init
if db.Migrator().HasTable("casbin_rule") {
    var n int64
    db.Table("casbin_rule").Count(&n)
    if n > 0 { /* already seeded - wipe before re-init */ }
}

Try / catch

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

Prevention

When it happens

Trigger: INSERT into the Casbin rule table fails during InitDB - leftover rows conflicting on re-init, table absent or with an old schema (v0/v1/v2 columns), DB connection loss during the large seed batch, or write-privilege denial.

Common situations: Re-running init on a DB that already contains casbin_rule rows; switching databases mid-project with stale schemas; adapter/table name misconfiguration in Casbin setup; low max_allowed_packet or timeouts on big inserts.

Related errors


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

Appendix: source

Thrown at server/source/system/casbin.go:424

		{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/editFileName", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/importURL", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/jwt/jsonInBlacklist", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/system/getSystemConfig", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/system/setSystemConfig", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "PUT"},
		{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "GET"},
		{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "DELETE"},
		{Ptype: "p", V0: "9528", V1: "/customer/customerList", V2: "GET"},
		{Ptype: "p", V0: "9528", V1: "/autoCode/createTemp", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/autoCode/mcpStatus", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/autoCode/mcpStart", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/autoCode/mcpStop", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/autoCode/mcpRoutes", V2: "POST"},
		{Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"},
	}
	if err := db.Create(&entities).Error; err != nil {
		return ctx, errors.Wrap(err, "Casbin 表 ("+i.InitializerName()+") 数据初始化失败!")
	}
	next := context.WithValue(ctx, i.InitializerName(), entities)
	return next, nil
}

func (i *initCasbin) DataInserted(ctx context.Context) bool {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	if errors.Is(db.Where(adapter.CasbinRule{Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"}).
		First(&adapter.CasbinRule{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
		return false
	}
	return true
}

View on GitHub (pinned to 3136500ef3)