{"record":{"id":"4a125385af7f4ad7","repo":"yiisoft/yii2","slug":"yii-db-sqlite-querybuilder-altercolumn-is-not-sup","errorCode":null,"errorMessage":"yii\\db\\sqlite\\QueryBuilder::alterColumn is not supported by SQLite.","messagePattern":"yii\\\\db\\\\sqlite\\\\QueryBuilder::alterColumn is not supported by SQLite\\.","errorType":"exception","errorClass":"yii\\base\\NotSupportedException","httpStatus":null,"severity":"error","filePath":"framework/db/sqlite/QueryBuilder.php","lineNumber":339,"sourceCode":"    public function renameTable($table, $newName)\n    {\n        return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' RENAME TO ' . $this->db->quoteTableName($newName);\n    }\n\n    /**\n     * Builds a SQL statement for changing the definition of a column.\n     * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.\n     * @param string $column the name of the column to be changed. The name will be properly quoted by the method.\n     * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract\n     * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept\n     * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null'\n     * will become 'varchar(255) not null'.\n     * @return string the SQL statement for changing the definition of a column.\n     * @throws NotSupportedException this is not supported by SQLite\n     */\n    public function alterColumn($table, $column, $type)\n    {\n        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');\n    }\n\n    /**\n     * Builds a SQL statement for adding a primary key constraint to an existing table.\n     * @param string $name the name of the primary key constraint.\n     * @param string $table the table that the primary key constraint will be added to.\n     * @param string|array $columns comma separated string or array of columns that the primary key will consist of.\n     * @return string the SQL statement for adding a primary key constraint to an existing table.\n     * @throws NotSupportedException this is not supported by SQLite\n     */\n    public function addPrimaryKey($name, $table, $columns)\n    {\n        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');\n    }\n\n    /**\n     * Builds a SQL statement for removing a primary key constraint to an existing table.\n     * @param string $name the name of the primary key constraint to be removed.","sourceCodeStart":321,"sourceCodeEnd":357,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/sqlite/QueryBuilder.php#L321-L357","documentation":"yii\\db\\sqlite\\QueryBuilder::alterColumn() always throws NotSupportedException: SQLite cannot change a column definition in place, since ALTER TABLE only supports RENAME TABLE and ADD COLUMN (plus limited RENAME COLUMN). Column type changes require the full table-rebuild pattern.","triggerScenarios":"Running a migration with $this->alterColumn('tbl', 'col', $this->string(500)) or calling $db->createCommand()->alterColumn(...) on a sqlite connection.","commonSituations":"Test suites using sqlite in-memory databases where a migration alters a column; evolving schemas on lightweight sqlite deployments; up/down migrations shared across drivers.","solutions":["On sqlite, rebuild the table: create a new table with the corrected column, copy data, drop the old one, rename.","Branch in migrations on $this->db->driverName === 'sqlite' and use the rebuild path there.","Catch NotSupportedException in generic migration runners.","Where possible, design the initial CREATE TABLE correctly instead of altering later on sqlite."],"exampleFix":"// before\n$this->alterColumn('user', 'name', $this->string(500));\n\n// after\nif ($this->db->driverName !== 'sqlite') {\n    $this->alterColumn('user', 'name', $this->string(500));\n} else {\n    $this->execute('CREATE TABLE user_new (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(500))');\n    $this->execute('INSERT INTO user_new (id, name) SELECT id, name FROM user');\n    $this->execute('DROP TABLE user');\n    $this->execute('ALTER TABLE user_new RENAME TO user');\n}","handlingStrategy":"try-catch","validationCode":"if ($db->driverName !== 'sqlite') {\n    $db->createCommand()->alterColumn($table, $column, $type)->execute();\n} else {\n    // SQLite: create new table with corrected DDL, copy data, rename\n}","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->alterColumn($table, $column, $type)->execute();\n} catch (\\yii\\db\\NotSupportedException $e) {\n    // SQLite: fall back to the recreate-and-copy rebuild pattern\n}","preventionTips":["Get CREATE TABLE definitions right up front for sqlite-only apps; avoid later column changes.","Implement a reusable sqlite table-rebuild helper instead of ad-hoc SQL.","Test migrations on every driver you ship to, not just production's."],"tags":["yii2","php","sqlite","database","alter-column","migration","not-supported"],"backgroundTag":"unsupported-driver-operation","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}