yiisoft/yii2 · error · yii\base\InvalidArgumentException

There is not sequence associated with table '$tableName'.

Error message

There is not sequence associated with table '$tableName'.

What it means

The second failure mode of yii\db\mssql\QueryBuilder::resetSequence() (framework/db/mssql/QueryBuilder.php:272): the table exists (getTableSchema returned a schema) but has no IDENTITY column, i.e. $table->sequenceName is null. DBCC CHECKIDENT can only reseed IDENTITY columns, so calling resetSequence on a table whose primary key is not an IDENTITY property throws InvalidArgumentException 'There is not sequence associated with table'.

Source

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

                $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;
        $tableNames = $this->db->getTableSchema($table) ? [$table] : $dbSchema->getTableNames($schema);
        $viewNames = $dbSchema->getViewNames($schema);
        $tableNames = array_diff($tableNames, $viewNames);

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Skip tables without an identity: check $db->getTableSchema($t)->sequenceName !== null before resetting
  2. If the table should auto-generate keys, alter the PK column to use IDENTITY(1,1), then retry
  3. Restrict fixture resets to the identity-backed tables that actually need reseeding

Example fix

// before
foreach ($tables as $t) {
    $db->createCommand()->resetSequence($t)->execute(); // throws on non-identity tables
}

// after
foreach ($tables as $t) {
    $schema = $db->getTableSchema($t);
    if ($schema !== null && $schema->sequenceName !== null) {
        $db->createCommand()->resetSequence($t)->execute();
    }
}
Defensive patterns

Strategy: validation

Validate before calling

$schema = $db->getTableSchema($table);
if ($schema !== null && $schema->sequenceName !== null) {
    $db->createCommand()->resetSequence($table)->execute();
} // else: no IDENTITY column -> DBCC CHECKIDENT not applicable

Try / catch

try {
    $db->createCommand()->resetSequence($table)->execute();
} catch (\yii\db\Exception $e) {
    \Yii::warning("Skipped sequence reset for '$table': " . $e->getMessage(), __METHOD__);
}

Prevention

When it happens

Trigger: $db->createCommand()->resetSequence('audit_log')->execute() where audit_log's PK is a plain INT (or GUID) without IDENTITY; generic fixture code resetting every table; sequences in SQL Server 2012+ (CREATE SEQUENCE) are unrelated — this driver path is IDENTITY-only.

Common situations: Fixture base classes looping over all tables; legacy tables with application-generated keys; porting data where the IDENTITY property was dropped.

Related errors


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