flipped-aurora/gin-vue-admin · critical
Casbin 表 (%s) 数据初始化失败!
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.
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)
Solutions
- Read the wrapped driver error (duplicate key, unknown column, access denied).
- Truncate/drop the Casbin rule table (or whole DB) and re-run InitDB for a clean seed.
- Confirm the Casbin adapter table name and schema match the configured model (i.InitializerName()).
- Verify DB privileges and connection stability for the bulk insert.
- 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
- Truncate casbin_rule before re-running init
- Confirm the Casbin adapter table name/schema matches the configured model
- Watch for max_allowed_packet/timeout limits on large batch inserts
- Use a DB account with write access to the policy table
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
- 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/c3356f64547d989c.
Report an issue: GitHub.