{"record":{"id":"c3356f64547d989c","repo":"flipped-aurora/gin-vue-admin","slug":"casbin-s","errorCode":null,"errorMessage":"Casbin 表 (%s) 数据初始化失败!","messagePattern":"Casbin 表 \\((.+?)\\) 数据初始化失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"server/source/system/casbin.go","lineNumber":424,"sourceCode":"\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/fileUploadAndDownload/editFileName\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/fileUploadAndDownload/importURL\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/jwt/jsonInBlacklist\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/system/getSystemConfig\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/system/setSystemConfig\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/customer/customer\", V2: \"PUT\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/customer/customer\", V2: \"GET\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/customer/customer\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/customer/customer\", V2: \"DELETE\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/customer/customerList\", V2: \"GET\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/autoCode/createTemp\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/autoCode/mcpStatus\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/autoCode/mcpStart\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/autoCode/mcpStop\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/autoCode/mcpRoutes\", V2: \"POST\"},\n\t\t{Ptype: \"p\", V0: \"9528\", V1: \"/user/getUserInfo\", V2: \"GET\"},\n\t}\n\tif err := db.Create(&entities).Error; err != nil {\n\t\treturn ctx, errors.Wrap(err, \"Casbin 表 (\"+i.InitializerName()+\") 数据初始化失败!\")\n\t}\n\tnext := context.WithValue(ctx, i.InitializerName(), entities)\n\treturn next, nil\n}\n\nfunc (i *initCasbin) DataInserted(ctx context.Context) bool {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn false\n\t}\n\tif errors.Is(db.Where(adapter.CasbinRule{Ptype: \"p\", V0: \"9528\", V1: \"/user/getUserInfo\", V2: \"GET\"}).\n\t\tFirst(&adapter.CasbinRule{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据\n\t\treturn false\n\t}\n\treturn true\n}\n","sourceCodeStart":406,"sourceCodeEnd":441,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/casbin.go#L406-L441","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// check casbin rule table state before init\nif db.Migrator().HasTable(\"casbin_rule\") {\n    var n int64\n    db.Table(\"casbin_rule\").Count(&n)\n    if n > 0 { /* already seeded - wipe before re-init */ }\n}","typeGuard":null,"tryCatchPattern":"if err := initdb(); err != nil {\n    if strings.Contains(err.Error(), \"casbin\") {\n        log.Printf(\"casbin seed failed: %v\", errors.Unwrap(err))\n    }\n    return err\n}","preventionTips":["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"],"tags":["database","casbin","seed-data","gorm"],"backgroundTag":"db-seed-insert-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}