{"record":{"id":"2efbde6dc0505b3a","repo":"crowdsecurity/crowdsec","slug":"unable-to-create-allowlist-w","errorCode":null,"errorMessage":"unable to create allowlist: %w","messagePattern":"unable to create allowlist: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/allowlists.go","lineNumber":36,"sourceCode":"\t\"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate\"\n\t\"github.com/crowdsecurity/crowdsec/pkg/models\"\n)\n\nconst allowlistExpireDecisionsBatchSize = 300\n\nfunc (c *Client) CreateAllowList(ctx context.Context, name string, description string, allowlistID string, fromConsole bool) (*ent.AllowList, error) {\n\tallowlist, err := c.Ent.AllowList.Create().\n\t\tSetName(name).\n\t\tSetFromConsole(fromConsole).\n\t\tSetDescription(description).\n\t\tSetAllowlistID(allowlistID).\n\t\tSave(ctx)\n\tif err != nil {\n\t\tif sqlgraph.IsUniqueConstraintError(err) {\n\t\t\treturn nil, fmt.Errorf(\"allowlist '%s' already exists\", name)\n\t\t}\n\n\t\treturn nil, fmt.Errorf(\"unable to create allowlist: %w\", err)\n\t}\n\n\treturn allowlist, nil\n}\n\nfunc (c *Client) DeleteAllowList(ctx context.Context, name string, fromConsole bool) error {\n\tnbDeleted, err := c.Ent.AllowListItem.Delete().Where(allowlistitem.HasAllowlistWith(allowlist.NameEQ(name), allowlist.FromConsoleEQ(fromConsole))).Exec(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to delete allowlist items: %w\", err)\n\t}\n\n\tc.Log.Debugf(\"deleted %d items from allowlist %s\", nbDeleted, name)\n\n\tnbDeleted, err = c.Ent.AllowList.\n\t\tDelete().\n\t\tWhere(allowlist.NameEQ(name), allowlist.FromConsoleEQ(fromConsole)).\n\t\tExec(ctx)\n\tif err != nil {","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/allowlists.go#L18-L54","documentation":"CreateAllowList's generic failure path: the insert of the allowlist row failed for any reason other than the name unique constraint. The raw database error is wrapped, so the underlying cause (connection, schema, permission) is preserved in the message chain.","triggerScenarios":"c.CreateAllowList(...) where Save(ctx) fails with a non-unique-constraint error: DB unreachable, schema out of date (missing allowlists table), INSERT permission denied, or context cancelled.","commonSituations":"Upgrading crowdsec without running cscli db migrate / schema migrations; DB container down when cscli allowlist create runs; read-only DB user.","solutions":["Read the wrapped %w cause in the error message for the exact DB error","Verify the database is reachable and migrations are applied (cscli db migrate)","Check INSERT grants on the allowlists table for the DB user","If the cause is a duplicate name, use the distinct 'already exists' handling"],"exampleFix":"// before\nal, err := client.CreateAllowList(ctx, name, desc, id, false)\n// after\nal, err := client.CreateAllowList(ctx, name, desc, id, false)\nif err != nil {\n    return fmt.Errorf(\"create allowlist: %w\", err) // inspect wrapped DB cause\n}","handlingStrategy":"try-catch","validationCode":"if err := client.Ent.AllowList.Query().Limit(1).Exec(ctx); err != nil {\n    return fmt.Errorf(\"database unavailable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"al, err := client.CreateAllowList(ctx, name, desc, id, false)\nif err != nil {\n    if !strings.Contains(err.Error(), \"already exists\") {\n        // generic DB failure: inspect wrapped cause, check DB health\n    }\n    return err\n}","preventionTips":["Run cscli db migrate after upgrading so the allowlists schema exists","Confirm the DB user has INSERT on allowlists","Verify DB reachability before scripted allowlist creation","Never discard the wrapped error — it contains the root DB cause"],"tags":["database","insert","allowlist"],"backgroundTag":"database-write-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}