yiisoft/yii2 · error · yii\base\NotSupportedException

yii\db\sqlite\QueryBuilder::dropPrimaryKey is not supported

Error message

yii\db\sqlite\QueryBuilder::dropPrimaryKey is not supported by SQLite.

What it means

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.

Source

Thrown at framework/db/sqlite/QueryBuilder.php:364

     * @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.
     */
    public function addUnique($name, $table, $columns)
    {
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
    }

    /**
     * {@inheritdoc}
     * @throws NotSupportedException this is not supported by SQLite.
     */
    public function dropUnique($name, $table)
    {
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Guard by driver and skip on sqlite.
  2. Rebuild the table without the PRIMARY KEY clause and copy the data back.
  3. Catch NotSupportedException in generic migration tooling and report the step as unsupported.
  4. Keep PK changes out of migrations that must run on sqlite.

Example fix

// before
$this->dropPrimaryKey('pk_product', 'product');

// after
if ($this->db->driverName !== 'sqlite') {
    $this->dropPrimaryKey('pk_product', 'product');
} else {
    $this->execute('CREATE TABLE product_new (id INTEGER NOT NULL, ...)');
    $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()->dropPrimaryKey($name, $table)->execute();
} else {
    // SQLite: rebuild table without PRIMARY KEY clause
}

Try / catch

try {
    $db->createCommand()->dropPrimaryKey($name, $table)->execute();
} catch (\yii\db\NotSupportedException $e) {
    // SQLite: recreate table without the PK and copy data
}

Prevention

When it happens

Trigger: Executing $this->dropPrimaryKey('pk_name', 'tbl') in a migration's down() or up() path, or $db->createCommand()->dropPrimaryKey(...), on a sqlite connection.

Common situations: Rollback paths of migrations that added PKs, run against sqlite test databases; schema-normalization migrations applied to sqlite deployments.

Related errors


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/e51744784e101278. Report an issue: GitHub.