{"record":{"id":"7accd988574b4b97","repo":"crowdsecurity/crowdsec","slug":"failed-creating-schema-resources-w","errorCode":null,"errorMessage":"failed creating schema resources: %w","messagePattern":"failed creating schema resources: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/database/database.go","lineNumber":117,"sourceCode":"\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to generate DB connection string: %w\", err)\n\t}\n\n\tdrv, err := getEntDriver(typ, dia, dbConnectionString, config)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed opening connection to %s: %w\", config.Type, err)\n\t}\n\n\tclient = ent.NewClient(ent.Driver(drv), entOpt)\n\n\tif config.LogLevel >= log.DebugLevel {\n\t\tlogger.Debugf(\"Enabling request debug\")\n\n\t\tclient = client.Debug()\n\t}\n\n\tif err = client.Schema.Create(ctx, dropLegacyIndex(\"decisions\", \"decision_value\")); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed creating schema resources: %w\", err)\n\t}\n\n\treturn &Client{\n\t\tEnt:              client,\n\t\tLog:              logger,\n\t\tType:             config.Type,\n\t\tWalMode:          config.UseWal,\n\t\tdecisionBulkSize: config.DecisionBulkSize,\n\t}, nil\n}\n\nfunc (c *Client) Close() error {\n\treturn c.Ent.Close()\n}\n","sourceCodeStart":99,"sourceCodeEnd":132,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/database.go#L99-L132","documentation":"NewClient wraps the error from client.Schema.Create (ent auto-migration) when the database schema tables/indexes could not be created or updated. The connection succeeded but DDL against the database failed — typically permissions or a corrupt/incompatible existing schema.","triggerScenarios":"First startup against a fresh DB where the user lacks CREATE privileges; startup against an existing DB whose schema conflicts (e.g. legacy index drop like dropLegacyIndex(\"decisions\",\"decision_value\") fails); read-only DB user; disk full on sqlite.","commonSituations":"Running crowdsec with a DB user that can connect but not ALTER/CREATE; a sqlite file corrupted after a crash; schema created by a much older crowdsec version that auto-migration can't reconcile; an old leftover index blocking migration.","solutions":["Grant the DB user CREATE/ALTER privileges on the crowdsec database.","Check the wrapped inner error for the failing statement and the conflicting table/index.","For sqlite, run integrity checks (PRAGMA integrity_check) or restore from backup.","Back up, then manually drop the conflicting legacy index/table before restarting."],"exampleFix":"// before (restricted user)\nGRANT SELECT, INSERT, UPDATE, DELETE ON crowdsec.* TO 'crowdsec'@'%';\n// after\nGRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP ON crowdsec.* TO 'crowdsec'@'%';","handlingStrategy":"try-catch","validationCode":"// verify privileges before startup, e.g. for mysql:\n// SHOW GRANTS FOR 'crowdsec'@'%';  must include CREATE, ALTER, INDEX, DROP","typeGuard":null,"tryCatchPattern":"client, err := database.NewClient(ctx, cfg)\nif err != nil {\n    var schemaErr error\n    if strings.Contains(err.Error(), \"failed creating schema resources\") {\n        return fmt.Errorf(\"db user lacks DDL rights or schema conflict: %w\", err)\n    }\n    return err\n}","preventionTips":["Grant DDL privileges to the crowdsec DB user","Back up the database before upgrading crowdsec","Check disk space for sqlite deployments","Review the wrapped SQL error to find conflicting indexes"],"tags":["database","migration","schema"],"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"}