{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php#L629-L665","documentation":"compileRenameIndex refuses to rename a PRIMARY KEY index on SQLite. SQLite has no ALTER statement to drop or replace a primary key definition, and renameIndex implements rename-by-drop-and-recreate, which is impossible for primaries. The guard fires after the source index is located, when $index['primary'] is true.","triggerScenarios":"Calling $table->renameIndex('sqlite_autoindex_...', 'new_name') (or the named primary key) on a SQLite table where the matched index is a primary key.","commonSituations":"Generic rename migrations that iterate over all indexes including primaries; renaming the implicit primary index created by $table->id(); cross-database migrations that name the primary on Postgres/MySQL but hit SQLite in tests.","solutions":["Exclude primary indexes from rename operations on SQLite (gate renameIndex by $index['primary'] === false using getIndexes()).","If the primary must be renamed, recreate the table with the new primary definition (SQLite only supports table-rebuild for primary-key changes).","Run the rename against Postgres/MySQL where primary keys can be dropped and re-added."],"exampleFix":"// before\n$table->renameIndex('users_pkey', 'users_id_primary');\n\n// after (skip primaries on SQLite)\nuse Illuminate\\Support\\Facades\\DB;\n\n$index = collect(DB::connection()->getSchemaBuilder()->getIndexes('users'))\n    ->firstWhere('name', 'users_pkey');\n\nif ($index && ! $index['primary']) {\n    $table->renameIndex('users_pkey', 'users_id_primary');\n}","handlingStrategy":"validation","validationCode":"use Illuminate\\Support\\Facades\\DB;\n\n$index = collect(DB::connection()->getSchemaBuilder()->getIndexes('users'))\n    ->firstWhere('name', 'users_pkey');\n\nif ($index && ! $index['primary']) {\n    $table->renameIndex('users_pkey', 'users_id_primary');\n}","typeGuard":"function indexIsRenamable(string $table, string $name): bool {\n    $i = collect(\\Illuminate\\Support\\Facades\\DB::connection()->getSchemaBuilder()->getIndexes($table))\n        ->firstWhere('name', $name);\n    return $i !== null && ! $i['primary'];\n}","tryCatchPattern":null,"preventionTips":["Exclude primary indexes from rename logic, especially in driver-portable migrations.","On SQLite, recreate the table to change a primary key rather than renameIndex.","Filter getIndexes() by primary/unique flags before calling renameIndex."],"tags":["database","schema","migrations","sqlite","indexes","primary-key","driver-unsupported","rename"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}