yiisoft/yii2 · error · InvalidArgumentException

Can't reset sequence for composite primary key in table: $ta

Error message

Can't reset sequence for composite primary key in table: $table

What it means

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.

Source

Thrown at framework/db/oci/QueryBuilder.php:155

    /**
     * {@inheritdoc}
     */
    public function executeResetSequence($table, $value = null)
    {
        $tableSchema = $this->db->getTableSchema($table);
        if ($tableSchema === null) {
            throw new InvalidArgumentException("Unknown table: $table");
        }
        if ($tableSchema->sequenceName === null) {
            throw new InvalidArgumentException("There is no sequence associated with table: $table");
        }

        if ($value !== null) {
            $value = (int) $value;
        } else {
            if (count($tableSchema->primaryKey) > 1) {
                throw new InvalidArgumentException("Can't reset sequence for composite primary key in table: $table");
            }
            // use master connection to get the biggest PK value
            $value = $this->db->useMaster(function (Connection $db) use ($tableSchema) {
                return $db->createCommand(
                    'SELECT MAX("' . $tableSchema->primaryKey[0] . '") FROM "' . $tableSchema->name . '"'
                )->queryScalar();
            }) + 1;
        }

        //Oracle needs at least two queries to reset sequence (see adding transactions and/or use alter method to avoid grants' issue?)
        $this->db->createCommand('DROP SEQUENCE "' . $tableSchema->sequenceName . '"')->execute();
        $this->db->createCommand('CREATE SEQUENCE "' . $tableSchema->sequenceName . '" START WITH ' . $value
            . ' INCREMENT BY 1 NOMAXVALUE NOCACHE')->execute();
    }

    /**
     * {@inheritdoc}
     */

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Pass an explicit value: $db->createCommand()->executeResetSequence('order_item', 1000)->execute().
  2. Restrict automatic sequence resets to tables with count($schema->primaryKey) === 1.
  3. For composite keys, compute your own seed from business rules and pass it explicitly.
  4. Skip resetting entirely - composite-key tables rarely use a sequence for all columns anyway.

Example fix

// before
$db->createCommand()->executeResetSequence('order_item')->execute();

// after
$schema = $db->getTableSchema('order_item', true);
if ($schema !== null && count($schema->primaryKey) > 1) {
    $db->createCommand()->executeResetSequence('order_item', 5000)->execute();
} else {
    $db->createCommand()->executeResetSequence('order_item')->execute();
}
Defensive patterns

Strategy: validation

Validate before calling

$schema = $db->getTableSchema($table, true);
if ($schema !== null && count($schema->primaryKey) > 1) {
    // composite PK: an explicit value is mandatory
    $db->createCommand()->executeResetSequence($table, $explicitValue)->execute();
} else {
    $db->createCommand()->executeResetSequence($table)->execute();
}

Try / catch

try {
    $db->createCommand()->executeResetSequence($table)->execute();
} catch (\yii\base\InvalidArgumentException $e) {
    if (str_contains($e->getMessage(), 'composite primary key')) {
        $db->createCommand()->executeResetSequence($table, 1)->execute(); // retry with explicit value
    }
}

Prevention

When it happens

Trigger: $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)).

Common situations: Fixture/ORM base classes that reset sequences for every table including junction tables with composite keys; generic cleanup scripts not aware of key shape.

Related errors


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