yiisoft/yii2 · error · yii\base\InvalidArgumentException

Table not found: $tableName

Error message

Table not found: $tableName

What it means

yii\db\mssql\QueryBuilder::resetSequence() (framework/db/mssql/QueryBuilder.php:246-272, implemented since 2.0.13) emits DBCC CHECKIDENT to reseed a table's IDENTITY counter, and starts by resolving the table via getTableSchema($tableName). A null schema means SQL Server has no such table from Yii's perspective and InvalidArgumentException 'Table not found' is thrown. It is reached through yii\db\Command::resetSequence() (framework/db/Command.php:971), commonly used after truncateTable() or when loading fixtures.

Source

Thrown at framework/db/mssql/QueryBuilder.php:269

            if ($value === null || $value === 1) {
                $key = $this->db->quoteColumnName(reset($table->primaryKey));
                $subSql = (new Query())
                    ->select('last_value')
                    ->from('sys.identity_columns')
                    ->where(['object_id' => new Expression("OBJECT_ID('{$tableName}')")])
                    ->andWhere(['IS NOT', 'last_value', null])
                    ->createCommand($this->db)
                    ->getRawSql();
                $sql = "SELECT COALESCE(MAX({$key}), CASE WHEN EXISTS({$subSql}) THEN 0 ELSE 1 END) FROM {$tableName}";
                $value = $this->db->createCommand($sql)->queryScalar();
            } else {
                $value = (int) $value;
            }

            return "DBCC CHECKIDENT ('{$tableName}', RESEED, {$value})";
        } elseif ($table === null) {
            throw new InvalidArgumentException("Table not found: $tableName");
        }

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

    /**
     * Builds a SQL statement for enabling or disabling integrity check.
     * @param bool $check whether to turn on or off the integrity check.
     * @param string $schema the schema of the tables.
     * @param string $table the table name.
     * @return string the SQL statement for checking integrity
     */
    public function checkIntegrity($check = true, $schema = '', $table = '')
    {
        /** @var Schema $dbSchema */
        $dbSchema = $this->db->getSchema();
        $enable = $check ? 'CHECK' : 'NOCHECK';
        $schema = $schema ?: $dbSchema->defaultSchema;

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Verify the fully qualified name first: if ($db->getTableSchema($tableName) === null) fail or skip with a log line
  2. Ensure migrations ran (./yii migrate/history) before running fixture/seed jobs
  3. Refresh cached metadata: $db->schema->refresh() or clear the cache component

Example fix

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

// after
if ($db->getTableSchema('dbo.order') !== null) {
    $db->createCommand()->resetSequence('dbo.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('order_items')->execute() where the table is missing or misnamed (including forgotten schema prefix like 'dbo.'); fixture teardown resetting sequences for tables a previous failed migration never created; stale schema cache after out-of-band DDL.

Common situations: Fixtures/tests that truncate and reseed identity tables; environments where migrations are partially applied; tables living in a non-default SQL Server schema referenced without the prefix.

Related errors


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