{"record":{"id":"8f5336f880751759","repo":"crowdsecurity/crowdsec","slug":"allowlist-s-already-exists","errorCode":null,"errorMessage":"allowlist '%s' already exists","messagePattern":"allowlist '(.+?)' already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/database/allowlists.go","lineNumber":33,"sourceCode":"\t\"github.com/crowdsecurity/crowdsec/pkg/database/ent/allowlist\"\n\t\"github.com/crowdsecurity/crowdsec/pkg/database/ent/allowlistitem\"\n\t\"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision\"\n\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().","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/allowlists.go#L15-L51","documentation":"CreateAllowList inserts a new allowlist row; a unique constraint on the allowlist name was violated, so the database refused the insert. Unlike the generic creation failure, this is a deterministic conflict: an allowlist with the same name already exists.","triggerScenarios":"c.CreateAllowList(ctx, name, description, allowlistID, fromConsole) where 'name' matches an existing allowlist row — e.g. cscli allowlist create with a duplicate name, or console-synced allowlist colliding with a locally created one.","commonSituations":"Re-running an init script that creates allowlists; creating an allowlist in cscli whose name was already pushed from the console; whitespace/case differences assumed unique but not distinct.","solutions":["List existing allowlists (cscli allowlists list) and pick a different name","Use the update/edit flow instead of create for the existing allowlist","If the stale duplicate is unwanted, delete it first (DeleteAllowList)","Check for hidden whitespace in the name argument"],"exampleFix":"// before\nal, err := client.CreateAllowList(ctx, \"my-allowlist\", \"desc\", alID, false)\n// after\nexisting, _ := client.GetAllowListByName(ctx, \"my-allowlist\")\nif existing != nil {\n    return fmt.Errorf(\"allowlist exists, editing instead\")\n}\nal, err := client.CreateAllowList(ctx, \"my-allowlist\", \"desc\", alID, false)","handlingStrategy":"validation","validationCode":"existing, err := client.GetAllowListByName(ctx, name)\nif err == nil && existing != nil {\n    return fmt.Errorf(\"allowlist %q already exists\", name)\n}","typeGuard":"func allowlistExists(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"already exists\")\n}","tryCatchPattern":"al, err := client.CreateAllowList(ctx, name, desc, id, false)\nif err != nil {\n    var notFound *ent.NotFoundError\n    if strings.Contains(err.Error(), \"already exists\") {\n        // fetch and reuse/edit the existing allowlist\n    }\n    return err\n}","preventionTips":["Check for an existing allowlist with the same name before creating","Use consistent naming conventions (and trim whitespace) for allowlist names","In automation, make creation idempotent: query first, create only if missing","Be aware console-synced allowlists share the same namespace as local ones"],"tags":["database","duplicate","allowlist","unique-constraint"],"backgroundTag":"entity-already-exists","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"}