{"id":"018be71007ce413b","repo":"knex/knex","slug":"alter-table-with-to-add-constraints-is-not-permitt","errorCode":null,"errorMessage":"Alter table with to add constraints is not permitted in SQLite","messagePattern":"Alter table with to add constraints is not permitted in SQLite","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/dialects/sqlite3/schema/sqlite-columncompiler.js","lineNumber":23,"sourceCode":"\nclass ColumnCompiler_SQLite3 extends ColumnCompiler {\n  constructor() {\n    super(...arguments);\n    this.modifiers = ['nullable', 'defaultTo'];\n    this._addCheckModifiers();\n  }\n\n  // Types\n  // -------\n\n  enu(allowed) {\n    return `text check (${this.formatter.wrap(\n      this.args[0]\n    )} in ('${allowed.join(\"', '\")}'))`;\n  }\n\n  _pushAlterCheckQuery(checkPredicate, constraintName) {\n    throw new Error(\n      `Alter table with to add constraints is not permitted in SQLite`\n    );\n  }\n\n  checkRegex(regexes, constraintName) {\n    return this._check(\n      `${this.formatter.wrap(\n        this.getColumnName()\n      )} REGEXP ${this.client._escapeBinding(regexes)}`,\n      constraintName\n    );\n  }\n}\n\nColumnCompiler_SQLite3.prototype.json = 'json';\nColumnCompiler_SQLite3.prototype.jsonb = 'json';\nColumnCompiler_SQLite3.prototype.double =\n  ColumnCompiler_SQLite3.prototype.decimal =","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/knex/knex/blob/e25d54bcb707714a17f5a5744eba5c4246bb4d1d/lib/dialects/sqlite3/schema/sqlite-columncompiler.js#L5-L41","documentation":"SQLite does not support adding CHECK constraints to an existing table via ALTER TABLE (the ALTER TABLE ADD CONSTRAINT syntax is not implemented by SQLite). knex's column compiler therefore overrides _pushAlterCheckQuery (the method that normally emits ALTER TABLE ... ADD CHECK for other dialects) to throw immediately on SQLite. This prevents emitting SQL that SQLite would reject.","triggerScenarios":"Using a column modifier that adds a CHECK constraint to an existing column on SQLite, e.g. .alter().check() or .checkRegex() inside an alterTable block that modifies an existing column (alterColumnsPrefix path). Adding a CHECK to a column via the checks API on an already-existing table.","commonSituations":"Sharing migration code between SQLite (dev/test) and PostgreSQL/MySQL (prod) where adding checks to existing columns is allowed. Using the fluent checks API without checking dialect support.","solutions":["Define CHECK constraints at table-creation time (inside createTable), which SQLite supports.","To add a CHECK to an existing SQLite table, rebuild it manually (knex.raw: create new table with check, copy data, drop, rename).","Skip the check-alter on SQLite via a dialect guard in your migration."],"exampleFix":"// before\nawait knex.schema.alterTable('t', (b) => {\n  b.string('email').alter().checkRegex(/^[^@]+@[^@]+$/);\n});\n// after\nawait knex.schema.alterTable('t', (b) => {\n  b.string('email').alter();\n});\n// or rebuild manually with a CHECK on the new table","handlingStrategy":"validation","validationCode":"function isSqlite(knex) { return /sqlite/i.test(knex.client.dialect || ''); }\nif (!isSqlite(knex)) {\n  await knex.schema.alterTable('t', (b) => b.string('c').alter().checkRegex(/x/));\n} else {\n  // define the CHECK at create time or rebuild the table manually\n}","typeGuard":"function supportsAlterCheck(knex) { return !/sqlite/i.test(knex.client.dialect || ''); }","tryCatchPattern":"try {\n  await knex.schema.alterTable('t', (b) => b.string('c').alter().check('c > 0'));\n} catch (e) {\n  if (/not permitted in SQLite/i.test(e.message)) {\n    // skip the check on SQLite or rebuild the table manually\n  } else throw e;\n}","preventionTips":["Define CHECK constraints at createTable time on SQLite.","Branch alter-column-check code by dialect.","Maintain a manual table-rebuild helper for adding checks to existing SQLite tables."],"tags":["sqlite","ddl","alter-table","check-constraint","dialect-unsupported"],"analyzedSha":"e25d54bcb707714a17f5a5744eba5c4246bb4d1d","analyzedAt":"2026-08-03T18:35:32.148Z","schemaVersion":2}