yiisoft/yii2 · error · yii\base\NotSupportedException
yii\db\sqlite\QueryBuilder::addPrimaryKey is not supported b
Error message
yii\db\sqlite\QueryBuilder::addPrimaryKey is not supported by SQLite.
What it means
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.
Source
Thrown at framework/db/sqlite/QueryBuilder.php:352
* @return string the SQL statement for changing the definition of a column.
* @throws NotSupportedException this is not supported by SQLite
*/
public function alterColumn($table, $column, $type)
{
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
}
/**
* Builds a SQL statement for adding a primary key constraint to an existing table.
* @param string $name the name of the primary key constraint.
* @param string $table the table that the primary key constraint will be added to.
* @param string|array $columns comma separated string or array of columns that the primary key will consist of.
* @return string the SQL statement for adding a primary key constraint to an existing table.
* @throws NotSupportedException this is not supported by SQLite
*/
public function addPrimaryKey($name, $table, $columns)
{
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
}
/**
* Builds a SQL statement for removing a primary key constraint to an existing table.
* @param string $name the name of the primary key constraint to be removed.
* @param string $table the table that the primary key constraint will be removed from.
* @return string the SQL statement for removing a primary key constraint from an existing table.
* @throws NotSupportedException this is not supported by SQLite
*/
public function dropPrimaryKey($name, $table)
{
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
}
/**
* {@inheritdoc}
* @throws NotSupportedException this is not supported by SQLite.
*/View on GitHub (pinned to 66f00d18a2)
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.
Example fix
// before
$this->addPrimaryKey('pk_product', 'product', ['id']);
// after
if ($this->db->driverName !== 'sqlite') {
$this->addPrimaryKey('pk_product', 'product', ['id']);
} else {
// rebuild with PRIMARY KEY inline, copy data, rename
$this->execute('CREATE TABLE product_new (id INTEGER NOT NULL PRIMARY KEY, ...)');
$this->execute('INSERT INTO product_new SELECT * FROM product');
$this->execute('DROP TABLE product');
$this->execute('ALTER TABLE product_new RENAME TO product');
} Defensive patterns
Strategy: try-catch
Validate before calling
if ($db->driverName !== 'sqlite') {
$db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
} else {
// SQLite: rebuild table with PRIMARY KEY declared in CREATE TABLE
} Try / catch
try {
$db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
} catch (\yii\db\NotSupportedException $e) {
// SQLite: recreate table with inline PRIMARY KEY and copy data
} Prevention
- Declare primary keys in initial CREATE TABLE statements for portable schemas.
- Branch constraint migrations on driver name.
- Smoke-test migrations on sqlite in CI.
When it happens
Trigger: A migration calling $this->addPrimaryKey('pk_name', 'tbl', ['id']) (or the equivalent Command call) while running on a sqlite connection.
Common situations: 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.
Related errors
- yii\db\sqlite\QueryBuilder::dropPrimaryKey is not supported
- yii\db\sqlite\QueryBuilder::dropForeignKey is not supported
- yii\db\sqlite\QueryBuilder::alterColumn is not supported by
- yii\db\sqlite\QueryBuilder::addUnique is not supported by SQ
- __METHOD__ is not supported by SQLite.
AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17).
Data as JSON: /api/errors/25c1c11ef4a9b356.
Report an issue: GitHub.