yiisoft/yii2 · error · yii\base\InvalidArgumentException

Table not found: $tableName

Error message

Table not found: $tableName

What it means

yii\db\cubrid\QueryBuilder::resetSequence() (framework/db/cubrid/QueryBuilder.php:131-148) resets a table's AUTO_INCREMENT counter and starts by resolving the table via $this->db->getTableSchema($tableName). When the lookup returns null the table does not exist from the schema's perspective and InvalidArgumentException 'Table not found' is thrown. It is reached through yii\db\Command::resetSequence() (framework/db/Command.php:971), typically called after truncating tables or loading fixtures to restart auto-increment.

Source

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

     * the next new row's primary key will have a value 1.
     * @return string the SQL statement for resetting sequence
     * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table.
     */
    public function resetSequence($tableName, $value = null)
    {
        $table = $this->db->getTableSchema($tableName);
        if ($table !== null && $table->sequenceName !== null) {
            $tableName = $this->db->quoteTableName($tableName);
            if ($value === null) {
                $key = reset($table->primaryKey);
                $value = (int) $this->db->createCommand("SELECT MAX(`$key`) FROM " . $this->db->schema->quoteTableName($tableName))->queryScalar() + 1;
            } else {
                $value = (int) $value;
            }

            return 'ALTER TABLE ' . $this->db->schema->quoteTableName($tableName) . " AUTO_INCREMENT=$value;";
        } elseif ($table === null) {
            throw new InvalidArgumentException("Table not found: $tableName");
        }

        throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.");
    }

    /**
     * {@inheritdoc}
     */
    public function buildLimit($limit, $offset)
    {
        $sql = '';
        // limit is not optional in CUBRID
        // https://www.cubrid.org/manual/en/9.3.0/sql/query/select.html#limit-clause
        // "You can specify a very big integer for row_count to display to the last row, starting from a specific row."
        if ($this->hasLimit($limit)) {
            $sql = 'LIMIT ' . $limit;
            if ($this->hasOffset($offset)) {
                $sql .= ' OFFSET ' . $offset;

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Verify the name first: if ($db->getTableSchema($tableName) === null) fail fast with a clear message or skip
  2. Ensure migrations/DDL ran before resetSequence (fixture load order)
  3. Refresh metadata when the cache is stale: $db->schema->refresh(), or clear the application cache component

Example fix

// before
$db->createCommand()->resetSequence('order')->execute(); // throws if table missing

// after
if ($db->getTableSchema('order') !== null) {
    $db->createCommand()->resetSequence('order')->execute();
}
Defensive patterns

Strategy: validation

Validate before calling

if ($db->getTableSchema($tableName) === null) {
    throw new \InvalidArgumentException("Cannot reset sequence: table '$tableName' does not exist.");
}
$db->createCommand()->resetSequence($tableName)->execute();

Try / catch

try {
    $db->createCommand()->resetSequence($tableName)->execute();
} catch (\yii\db\Exception $e) {
    \Yii::warning('resetSequence failed: ' . $e->getMessage(), __METHOD__);
}

Prevention

When it happens

Trigger: $db->createCommand()->resetSequence('user_typo')->execute(); calling resetSequence in fixture/migration code before the table was created; table-name case/spelling mismatch; stale schema cache holding pre-create metadata.

Common situations: Test fixtures resetting auto-increment after truncateTable(); CI runs against a database where migrations have not been applied; schema cache not invalidated after DDL executed outside the application.

Related errors


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