{"record":{"id":"958bbd67d7148b78","repo":"litedb-org/LiteDB","slug":"0-958bbd","errorCode":"0","errorMessage":"Pragma COLLATION is read only. Use Rebuild options.","messagePattern":"Pragma COLLATION is read only\\. Use Rebuild options\\.","errorType":"exception","errorClass":"LiteException","httpStatus":null,"severity":"error","filePath":"LiteDB/Engine/EnginePragmas.cs","lineNumber":92,"sourceCode":"\n            _pragmas = new Dictionary<string, Pragma>(StringComparer.OrdinalIgnoreCase)\n            {\n                [Engine.Pragmas.USER_VERSION] = new Pragma\n                {\n                    Name = Engine.Pragmas.USER_VERSION,\n                    Get = () => this.UserVersion,\n                    Set = (v) => this.UserVersion = v.AsInt32,\n                    Read = (b) => this.UserVersion = b.ReadInt32(P_USER_VERSION),\n                    Validate = (v, h) => { },\n                    Write = (b) => b.Write(this.UserVersion, P_USER_VERSION)\n                },\n                [Engine.Pragmas.COLLATION] = new Pragma\n                {\n                    Name = Engine.Pragmas.COLLATION,\n                    Get = () => this.Collation.ToString(),\n                    Set = (v) => this.Collation = new Collation(v.AsString),\n                    Read = (b) => this.Collation = new Collation(b.ReadInt32(P_COLLATION_LCID), (CompareOptions)b.ReadInt32(P_COLLATION_SORT)),\n                    Validate = (v, h) => { throw new LiteException(0, \"Pragma COLLATION is read only. Use Rebuild options.\"); },\n                    Write = (b) =>\n                    {\n                        b.Write(this.Collation.LCID, P_COLLATION_LCID);\n                        b.Write((int)this.Collation.SortOptions, P_COLLATION_SORT);\n                    }\n                },\n                [Engine.Pragmas.TIMEOUT] = new Pragma\n                {\n                    Name = Engine.Pragmas.TIMEOUT,\n                    Get = () => (int)this.Timeout.TotalSeconds,\n                    Set = (v) => this.Timeout = TimeSpan.FromSeconds(v.AsInt32),\n                    Read = (b) => this.Timeout = TimeSpan.FromSeconds(b.ReadInt32(P_TIMEOUT)),\n                    Validate = (v, h) => { if (v <= 0) throw new LiteException(0, \"Pragma TIMEOUT must be greater than zero\"); },\n                    Write = (b) => b.Write((int)this.Timeout.TotalSeconds, P_TIMEOUT)\n                },\n                [Engine.Pragmas.LIMIT_SIZE] = new Pragma\n                {\n                    Name = Engine.Pragmas.LIMIT_SIZE,","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/litedb-org/LiteDB/blob/f906a5f850678719e39a39a006cb66dcae563cfa/LiteDB/Engine/EnginePragmas.cs#L74-L110","documentation":"Thrown when a user attempts to change the COLLATION pragma at runtime via Pragma('COLLATION', value) or the equivalent SQL. Collation is persisted into the database header page at creation time and drives every index comparison; changing it in-place would invalidate all existing B-tree orderings. The engine marks its Validate callback as an unconditional throw, so any validated set is rejected.","triggerScenarios":"Executing PRAGMA COLLATION = '...' via SQL or calling engine.Pragma(Pragma.COLLATION, value) after the database has been created. The Validate delegate runs during Set(name, value, validate: true).","commonSituations":"Migrating a database to a new locale without realizing collation is immutable post-creation; scripting a generic 'apply pragmas' routine that blindly sets all pragmas; attempting culture-specific sorting on an existing DB.","solutions":["Set collation at database creation time via EngineSettings.Collation or the connection string Collation parameter.","To change collation on an existing database, use the Rebuild API with a new collation (db.Rebuild(options => options.Collation = new Collation(...))).","Remove COLLATION from any generic pragma-application loop.","If you only need culture-aware comparison for a query, transform/sort in application code rather than changing the DB collation."],"exampleFix":"// before — tries to set at runtime\ndb.Pragma(\"COLLATION\", \"en-US/None\"); // throws\n\n// after — set at creation\nvar db = new LiteDatabase(\"connection string { collation=en-US/None }\");\n// or rebuild to change later\ndb.Rebuild(c => c.Collation = new Collation(\"en-US/None\"));","handlingStrategy":"validation","validationCode":"// Never set COLLATION at runtime; set it at creation or via Rebuild.\npublic void ApplyPragma(LiteDatabase db, string name, BsonValue value)\n{\n    if (string.Equals(name, LiteDB.Engine.Pragmas.COLLATION, StringComparison.OrdinalIgnoreCase))\n    {\n        // skip — read-only pragma\n        return;\n    }\n    db.Pragma(name, value);\n}","typeGuard":"static bool IsReadOnlyPragma(string name) =>\n    string.Equals(name, LiteDB.Engine.Pragmas.COLLATION, StringComparison.OrdinalIgnoreCase);","tryCatchPattern":"try\n{\n    db.Pragma(LiteDB.Engine.Pragmas.COLLATION, collationString);\n}\ncatch (LiteException ex) when (ex.Message.Contains(\"COLLATION is read only\"))\n{\n    // Use Rebuild instead:\n    // db.Rebuild(c => c.Collation = new Collation(collationString));\n    throw new InvalidOperationException(\"Collation is immutable post-creation. Use Rebuild to change it.\", ex);\n}","preventionTips":["Set collation at database creation time only.","Filter COLLATION out of generic pragma-application loops.","To change collation on an existing DB, use Rebuild.","Document the chosen collation in the project to prevent accidental overrides."],"tags":["pragma","collation","configuration","litedb-engine"],"backgroundTag":null,"analyzedSha":"f906a5f850678719e39a39a006cb66dcae563cfa","analyzedAt":"2026-08-13T21:56:30.148Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}