{"record":{"id":"4902e0bc6ce0ba35","repo":"yiisoft/yii2","slug":"yii-db-sqlite-querybuilder-addunique-is-not-suppo","errorCode":null,"errorMessage":"yii\\db\\sqlite\\QueryBuilder::addUnique is not supported by SQLite.","messagePattern":"yii\\\\db\\\\sqlite\\\\QueryBuilder::addUnique is not supported by SQLite\\.","errorType":"exception","errorClass":"yii\\base\\NotSupportedException","httpStatus":null,"severity":"error","filePath":"framework/db/sqlite/QueryBuilder.php","lineNumber":373,"sourceCode":"    /**\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.');\n    }\n\n    /**\n     * {@inheritdoc}\n     * @throws NotSupportedException this is not supported by SQLite.\n     */\n    public function addCheck($name, $table, $expression)\n    {\n        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/sqlite/QueryBuilder.php#L355-L391","documentation":"yii\\db\\sqlite\\QueryBuilder::addUnique() unconditionally throws NotSupportedException: SQLite cannot add a UNIQUE constraint to an existing table with ALTER TABLE. Unique indexes (CREATE UNIQUE INDEX) are supported, but the named-constraint API is not, so Yii rejects the operation outright.","triggerScenarios":"A migration calling $this->addUnique('uq_name', 'tbl', ['email']) or the equivalent Command call while the connection is sqlite - commonly the test/CI database.","commonSituations":"Cross-driver migrations adding unique constraints after table creation; sqlite in-memory test databases running the full migration set; schema-tidying migrations written for production MySQL/PostgreSQL.","solutions":["On sqlite, create a unique index instead: $this->createIndex('uq_name', 'tbl', 'email', true).","Branch on $this->db->driverName === 'sqlite' in the migration.","Declare UNIQUE inline in the original CREATE TABLE for sqlite-first schemas.","Catch NotSupportedException in driver-agnostic runners."],"exampleFix":"// before\n$this->addUnique('uq_user_email', 'user', ['email']);\n\n// after\nif ($this->db->driverName !== 'sqlite') {\n    $this->addUnique('uq_user_email', 'user', ['email']);\n} else {\n    $this->createIndex('uq_user_email', 'user', 'email', true); // UNIQUE index\n}","handlingStrategy":"try-catch","validationCode":"if ($db->driverName !== 'sqlite') {\n    $db->createCommand()->addUnique($name, $table, $columns)->execute();\n} else {\n    // SQLite equivalent: a UNIQUE index\n    $db->createCommand()->createIndex($name, $table, $columns, true)->execute();\n}","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->addUnique($name, $table, $columns)->execute();\n} catch (\\yii\\db\\NotSupportedException $e) {\n    // SQLite: fall back to CREATE UNIQUE INDEX\n    $db->createCommand()->createIndex($name, $table, $columns, true)->execute();\n}","preventionTips":["Prefer unique indexes over named unique constraints for portable schemas.","Branch uniqueness migrations on driver name.","Verify unique coverage in tests rather than assuming constraint semantics."],"tags":["yii2","php","sqlite","database","unique-constraint","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"}