{"record":{"id":"e51744784e101278","repo":"yiisoft/yii2","slug":"yii-db-sqlite-querybuilder-dropprimarykey-is-not","errorCode":null,"errorMessage":"yii\\db\\sqlite\\QueryBuilder::dropPrimaryKey is not supported by SQLite.","messagePattern":"yii\\\\db\\\\sqlite\\\\QueryBuilder::dropPrimaryKey is not supported by SQLite\\.","errorType":"exception","errorClass":"yii\\base\\NotSupportedException","httpStatus":null,"severity":"error","filePath":"framework/db/sqlite/QueryBuilder.php","lineNumber":364,"sourceCode":"     * @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.\n     * @param string $table the table that the primary key constraint will be removed from.\n     * @return string the SQL statement for removing a primary key constraint from an existing table.\n     * @throws NotSupportedException this is not supported by SQLite\n     */\n    public function dropPrimaryKey($name, $table)\n    {\n        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');\n    }\n\n    /**\n     * {@inheritdoc}\n     * @throws NotSupportedException this is not supported by SQLite.\n     */\n    public function addUnique($name, $table, $columns)\n    {\n        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');\n    }\n\n    /**\n     * {@inheritdoc}\n     * @throws NotSupportedException this is not supported by SQLite.\n     */\n    public function dropUnique($name, $table)\n    {\n        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/sqlite/QueryBuilder.php#L346-L382","documentation":"yii\\db\\sqlite\\QueryBuilder::dropPrimaryKey() always throws NotSupportedException: SQLite has no ALTER TABLE ... DROP PRIMARY KEY form, so a PK cannot be removed from an existing table. The only supported route is recreating the table without the constraint.","triggerScenarios":"Executing $this->dropPrimaryKey('pk_name', 'tbl') in a migration's down() or up() path, or $db->createCommand()->dropPrimaryKey(...), on a sqlite connection.","commonSituations":"Rollback paths of migrations that added PKs, run against sqlite test databases; schema-normalization migrations applied to sqlite deployments.","solutions":["Guard by driver and skip on sqlite.","Rebuild the table without the PRIMARY KEY clause and copy the data back.","Catch NotSupportedException in generic migration tooling and report the step as unsupported.","Keep PK changes out of migrations that must run on sqlite."],"exampleFix":"// before\n$this->dropPrimaryKey('pk_product', 'product');\n\n// after\nif ($this->db->driverName !== 'sqlite') {\n    $this->dropPrimaryKey('pk_product', 'product');\n} else {\n    $this->execute('CREATE TABLE product_new (id INTEGER NOT NULL, ...)');\n    $this->execute('INSERT INTO product_new SELECT * FROM product');\n    $this->execute('DROP TABLE product');\n    $this->execute('ALTER TABLE product_new RENAME TO product');\n}","handlingStrategy":"try-catch","validationCode":"if ($db->driverName !== 'sqlite') {\n    $db->createCommand()->dropPrimaryKey($name, $table)->execute();\n} else {\n    // SQLite: rebuild table without PRIMARY KEY clause\n}","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->dropPrimaryKey($name, $table)->execute();\n} catch (\\yii\\db\\NotSupportedException $e) {\n    // SQLite: recreate table without the PK and copy data\n}","preventionTips":["Keep PK lifecycle changes out of migrations that run on sqlite.","Branch down() paths on driver name just like up() paths.","Use a shared rebuild helper for all unsupported ALTER operations."],"tags":["yii2","php","sqlite","database","primary-key","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"}