yiisoft/yii2 · error · InvalidArgumentException

Unknown table: $table

Error message

Unknown table: $table

What it means

yii\db\oci\QueryBuilder::executeResetSequence() drops and recreates the Oracle sequence that backs a table's primary key, so it first resolves the table through $this->db->getTableSchema($table). A null schema produces InvalidArgumentException 'Unknown table'. Unlike the SQL-building resetSequence(), this variant executes multiple statements directly.

Source

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

     * Builds a SQL statement for dropping an index.
     *
     * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
     * @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;
        }

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Verify visibility first: $db->getTableSchema($table, true) must return a schema.
  2. Use the schema-qualified, correctly-cased name exactly as introspection reports it (typically uppercase for unquoted Oracle names).
  3. Confirm the connection user matches the schema, or grant/select access and add the prefix.
  4. Run migrations or imports that create the table before resetting its sequence.

Example fix

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

// after
$table = 'APP.ITEM'; // schema-qualified, uppercase
if ($db->getTableSchema($table, true) === null) {
    throw new InvalidArgumentException("Unknown table: $table");
}
$db->createCommand()->executeResetSequence($table);
Defensive patterns

Strategy: validation

Validate before calling

// Oracle: verify schema-qualified, correctly-cased table first
if ($db->getTableSchema($table, true) === null) {
    throw new InvalidArgumentException("Unknown table: $table");
}
$db->createCommand()->executeResetSequence($table)->execute();

Type guard

use yii\db\TableSchema;

function tableSchemaOrFail(?TableSchema $schema, string $name): TableSchema
{
    if ($schema === null) {
        throw new InvalidArgumentException("Unknown table: $name");
    }
    return $schema;
}

Try / catch

try {
    $db->createCommand()->executeResetSequence($table)->execute();
} catch (\yii\base\InvalidArgumentException $e) {
    // recheck with refresh: $db->getTableSchema($table, true)
}

Prevention

When it happens

Trigger: Calling $db->createCommand()->executeResetSequence('tbl') where 'tbl' is misspelled, lives in a different Oracle schema than the connected user, or uses different case (Oracle upper-cases unquoted identifiers).

Common situations: Connecting as a user different from the table owner; forgetting the SCHEMA.TABLE prefix in multi-schema Oracle setups; fixture code assuming the same defaults as the MySQL driver; lowercase table names on a case-sensitive Oracle installation.

Related errors


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