{"record":{"id":"c228b82a12209b73","repo":"yiisoft/yii2","slug":"can-t-reset-sequence-for-composite-primary-key-in","errorCode":null,"errorMessage":"Can't reset sequence for composite primary key in table: $table","messagePattern":"Can't reset sequence for composite primary key in table: \\$table","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"framework/db/oci/QueryBuilder.php","lineNumber":155,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     */\n    public function executeResetSequence($table, $value = null)\n    {\n        $tableSchema = $this->db->getTableSchema($table);\n        if ($tableSchema === null) {\n            throw new InvalidArgumentException(\"Unknown table: $table\");\n        }\n        if ($tableSchema->sequenceName === null) {\n            throw new InvalidArgumentException(\"There is no sequence associated with table: $table\");\n        }\n\n        if ($value !== null) {\n            $value = (int) $value;\n        } else {\n            if (count($tableSchema->primaryKey) > 1) {\n                throw new InvalidArgumentException(\"Can't reset sequence for composite primary key in table: $table\");\n            }\n            // use master connection to get the biggest PK value\n            $value = $this->db->useMaster(function (Connection $db) use ($tableSchema) {\n                return $db->createCommand(\n                    'SELECT MAX(\"' . $tableSchema->primaryKey[0] . '\") FROM \"' . $tableSchema->name . '\"'\n                )->queryScalar();\n            }) + 1;\n        }\n\n        //Oracle needs at least two queries to reset sequence (see adding transactions and/or use alter method to avoid grants' issue?)\n        $this->db->createCommand('DROP SEQUENCE \"' . $tableSchema->sequenceName . '\"')->execute();\n        $this->db->createCommand('CREATE SEQUENCE \"' . $tableSchema->sequenceName . '\" START WITH ' . $value\n            . ' INCREMENT BY 1 NOMAXVALUE NOCACHE')->execute();\n    }\n\n    /**\n     * {@inheritdoc}\n     */","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/oci/QueryBuilder.php#L137-L173","documentation":"When executeResetSequence() is called without an explicit $value, Oracle's driver computes MAX(pk[0]) + 1 to reseed the sequence. For a composite primary key that computation is meaningless, so the oci QueryBuilder rejects it with InvalidArgumentException. Passing a non-null $value skips the branch entirely.","triggerScenarios":"$db->createCommand()->executeResetSequence('tbl') with $value omitted (null) on a table whose TableSchema reports more than one primary-key column (e.g. PRIMARY KEY (order_id, line_no)).","commonSituations":"Fixture/ORM base classes that reset sequences for every table including junction tables with composite keys; generic cleanup scripts not aware of key shape.","solutions":["Pass an explicit value: $db->createCommand()->executeResetSequence('order_item', 1000)->execute().","Restrict automatic sequence resets to tables with count($schema->primaryKey) === 1.","For composite keys, compute your own seed from business rules and pass it explicitly.","Skip resetting entirely - composite-key tables rarely use a sequence for all columns anyway."],"exampleFix":"// before\n$db->createCommand()->executeResetSequence('order_item')->execute();\n\n// after\n$schema = $db->getTableSchema('order_item', true);\nif ($schema !== null && count($schema->primaryKey) > 1) {\n    $db->createCommand()->executeResetSequence('order_item', 5000)->execute();\n} else {\n    $db->createCommand()->executeResetSequence('order_item')->execute();\n}","handlingStrategy":"validation","validationCode":"$schema = $db->getTableSchema($table, true);\nif ($schema !== null && count($schema->primaryKey) > 1) {\n    // composite PK: an explicit value is mandatory\n    $db->createCommand()->executeResetSequence($table, $explicitValue)->execute();\n} else {\n    $db->createCommand()->executeResetSequence($table)->execute();\n}","typeGuard":null,"tryCatchPattern":"try {\n    $db->createCommand()->executeResetSequence($table)->execute();\n} catch (\\yii\\base\\InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'composite primary key')) {\n        $db->createCommand()->executeResetSequence($table, 1)->execute(); // retry with explicit value\n    }\n}","preventionTips":["Check count($schema->primaryKey) before omitting the value argument.","Treat junction/composite-key tables as non-resettable in fixture logic.","Pass explicit seed values in tests for deterministic IDs."],"tags":["yii2","php","oracle","oci","database","sequence","composite-primary-key"],"backgroundTag":"composite-primary-key-unsupported","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}