{"record":{"id":"25c1c11ef4a9b356","repo":"yiisoft/yii2","slug":"yii-db-sqlite-querybuilder-addprimarykey-is-not-s","errorCode":null,"errorMessage":"yii\\db\\sqlite\\QueryBuilder::addPrimaryKey is not supported by SQLite.","messagePattern":"yii\\\\db\\\\sqlite\\\\QueryBuilder::addPrimaryKey is not supported by SQLite\\.","errorType":"exception","errorClass":"yii\\base\\NotSupportedException","httpStatus":null,"severity":"error","filePath":"framework/db/sqlite/QueryBuilder.php","lineNumber":352,"sourceCode":"     * @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.\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     */","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/sqlite/QueryBuilder.php#L334-L370","documentation":"yii\\db\\sqlite\\QueryBuilder::addPrimaryKey() unconditionally throws NotSupportedException because SQLite cannot add a primary-key constraint to an existing table via ALTER TABLE - the PK must be declared in the original CREATE TABLE statement.","triggerScenarios":"A migration calling $this->addPrimaryKey('pk_name', 'tbl', ['id']) (or the equivalent Command call) while running on a sqlite connection.","commonSituations":"Cross-driver migrations that add PKs after table creation (common when normalizing legacy schemas) executed against sqlite test databases; CI running full migration chains on sqlite.","solutions":["Skip the step on sqlite: if ($this->db->driverName === 'sqlite') return;","Rebuild the table with the PRIMARY KEY declared in CREATE TABLE and copy the data.","Declare the PK at initial table creation so no later add is needed.","Catch NotSupportedException in driver-agnostic migration runners."],"exampleFix":"// before\n$this->addPrimaryKey('pk_product', 'product', ['id']);\n\n// after\nif ($this->db->driverName !== 'sqlite') {\n    $this->addPrimaryKey('pk_product', 'product', ['id']);\n} else {\n    // rebuild with PRIMARY KEY inline, copy data, rename\n    $this->execute('CREATE TABLE product_new (id INTEGER NOT NULL PRIMARY KEY, ...)');\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()->addPrimaryKey($name, $table, $columns)->execute();\n} else {\n    // SQLite: rebuild table with PRIMARY KEY declared in CREATE TABLE\n}","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();\n} catch (\\yii\\db\\NotSupportedException $e) {\n    // SQLite: recreate table with inline PRIMARY KEY and copy data\n}","preventionTips":["Declare primary keys in initial CREATE TABLE statements for portable schemas.","Branch constraint migrations on driver name.","Smoke-test migrations on sqlite in CI."],"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"}