{"id":"9b52e753fe9d8804","repo":"laravel/framework","slug":"sqlite-does-not-support-altering-primary-keys","errorCode":null,"errorMessage":"SQLite does not support altering primary keys.","messagePattern":"SQLite does not support altering primary keys\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php","lineNumber":647,"sourceCode":"     *\n     * @param  \\Illuminate\\Database\\Schema\\Blueprint  $blueprint\n     * @param  \\Illuminate\\Support\\Fluent  $command\n     * @return array\n     *\n     * @throws \\RuntimeException\n     */\n    public function compileRenameIndex(Blueprint $blueprint, Fluent $command)\n    {\n        $indexes = $this->connection->getSchemaBuilder()->getIndexes($blueprint->getTable());\n\n        $index = Arr::first($indexes, fn ($index) => $index['name'] === $command->from);\n\n        if (! $index) {\n            throw new RuntimeException(\"Index [{$command->from}] does not exist.\");\n        }\n\n        if ($index['primary']) {\n            throw new RuntimeException('SQLite does not support altering primary keys.');\n        }\n\n        if ($index['unique']) {\n            return [\n                $this->compileDropUnique($blueprint, new IndexDefinition(['index' => $index['name']])),\n                $this->compileUnique($blueprint,\n                    new IndexDefinition(['index' => $command->to, 'columns' => $index['columns']])\n                ),\n            ];\n        }\n\n        return [\n            $this->compileDropIndex($blueprint, new IndexDefinition(['index' => $index['name']])),\n            $this->compileIndex($blueprint,\n                new IndexDefinition(['index' => $command->to, 'columns' => $index['columns']])\n            ),\n        ];\n    }","sourceCodeStart":629,"sourceCodeEnd":665,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php#L629-L665","documentation":"SQLiteGrammar.compileRenameIndex() throws RuntimeException when the matched index is a PRIMARY KEY. SQLite's primary keys are tied to the table's rowid and cannot be altered via the drop+recreate rename path the grammar uses for normal indexes. This fires AFTER the index lookup succeeds but identifies it as primary.","triggerScenarios":"Calling $table->renameIndex('users_pkey', 'users_id_new') (or the auto-generated primary index name) on a SQLite connection. The lookup finds the index but $index['primary'] is true, so SQLite refuses the rename.","commonSituations":"Renaming what is actually a primary key index; sharing rename migrations between drivers (Postgres/MySQL allow renaming primary indexes in some forms); schema-diff tools emitting primary-index renames.","solutions":["Do not rename a primary key index on SQLite — leave primary keys as-is or recreate the table with the new constraint.","If a rename is essential, build a new table with the desired primary key, copy data, and swap (SQLite table-rebuild pattern).","Skip the rename for SQLite via a driver guard and only rename on drivers that permit it."],"exampleFix":"// before (throws on SQLite)\nSchema::table('users', function (Blueprint $table) {\n    $table->renameIndex('users_pkey', 'users_pk');\n});\n\n// after — skip on SQLite, rebuild table if rename is required\nSchema::table('users', function (Blueprint $table) {\n    if ($table->getConnection()->getDriverName() !== 'sqlite') {\n        $table->renameIndex('users_pkey', 'users_pk');\n    }\n});","handlingStrategy":"validation","validationCode":"if (Schema::getConnection()->getDriverName() !== 'sqlite') {\n    $table->renameIndex('users_pkey', 'users_pk');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never attempt to rename a primary-key index on SQLite.","If a primary key rename is required on SQLite, rebuild the table.","Driver-guard primary-key rename migrations."],"tags":["database","schema","sqlite","migrations","primary-key"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}