yiisoft/yii2 · error · InvalidArgumentException

There is no sequence associated with table: $table

Error message

There is no sequence associated with table: $table

What it means

The Oracle executeResetSequence() drops and recreates TableSchema::sequenceName, so the table schema must expose a backing sequence. This exception fires when the schema was found but ->sequenceName is null: the primary key is not populated by an Oracle sequence (no IDENTITY column, no sequence+trigger pair).

Source

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

     * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
     * @return string the SQL statement for dropping an index.
     */
    public function dropIndex($name, $table)
    {
        return 'DROP INDEX ' . $this->db->quoteTableName($name);
    }

    /**
     * {@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();

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Check $schema->sequenceName !== null before calling executeResetSequence().
  2. If auto-numbering is intended, convert the PK to an IDENTITY column or a documented sequence+trigger setup.
  3. Skip the reset for tables with application-assigned keys - there is no sequence to reset.
  4. Pass an explicit $value only when a sequence actually exists.

Example fix

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

// after
$schema = $db->getTableSchema('audit_log', true);
if ($schema !== null && $schema->sequenceName !== null) {
    $db->createCommand()->executeResetSequence('audit_log');
}
Defensive patterns

Strategy: validation

Validate before calling

$schema = $db->getTableSchema($table, true);
if ($schema === null || $schema->sequenceName === null) {
    return; // no sequence-backed PK: nothing to reset
}
$db->createCommand()->executeResetSequence($table)->execute();

Try / catch

try {
    $db->createCommand()->executeResetSequence($table)->execute();
} catch (\yii\base\InvalidArgumentException $e) {
    if (str_contains($e->getMessage(), 'no sequence')) {
        // PK not sequence-backed; skip
    }
}

Prevention

When it happens

Trigger: Calling $db->createCommand()->executeResetSequence('tbl') on a table whose PK is assigned by the application (UUIDs, natural keys) or populated by a trigger Yii cannot associate with a sequence during introspection.

Common situations: Legacy Oracle schemas with manually numbered PKs; tables using triggers Yii's introspection does not recognize as sequence-backed; fixture code ported from MySQL that resets every table.

Related errors


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