yiisoft/yii2 · error · yii\base\NotSupportedException

MySQL < 8.0.16 does not support check constraints.

Error message

MySQL < 8.0.16 does not support check constraints.

What it means

yii\db\mysql\Schema::loadTableChecks() introspects CHECK constraints from INFORMATION_SCHEMA, but MySQL only started storing them in version 8.0.16 - older versions parse and silently ignore CHECK clauses. On non-MariaDB servers below 8.0.16 the method throws NotSupportedException. MariaDB is exempted because its version string contains 'MariaDb' and it has long supported check constraints.

Solutions

  1. Upgrade the MySQL server to 8.0.16 or newer.
  2. Gate the call: skip getTableChecks() when $db->getServerVersion() is below 8.0.16 and the driver is mysql.
  3. Catch NotSupportedException in cross-version introspection code and treat checks as an empty list.
  4. On MariaDB no action is needed - the version guard already excludes it.

Example fix

// before
$checks = $db->getTableChecks('order');

// after
$version = $db->getServerVersion();
$isMySql = stripos($version, 'MariaDb') === false;
$checks = ($isMySql && version_compare($version, '8.0.16', '<'))
    ? []
    : $db->getTableChecks('order');
Defensive patterns

Strategy: validation

Validate before calling

function mysqlSupportsCheckIntrospection(\yii\db\Connection $db): bool
{
    $version = $db->getServerVersion();
    return stripos($version, 'MariaDb') !== false
        || version_compare($version, '8.0.16', '>=');
}

$checks = mysqlSupportsCheckIntrospection($db) ? $db->getTableChecks('order') : [];

Try / catch

try {
    $checks = $db->getTableChecks('order');
} catch (\yii\db\NotSupportedException $e) {
    $checks = []; // MySQL < 8.0.16: CHECK constraints are parsed but not stored
}

Prevention

When it happens

Trigger: Calling $db->getTableChecks('tbl') on MySQL 5.6/5.7/8.0.0-8.0.15, directly or from generic code that iterates all constraint types (Gii model generation, schema dump tools, debug panels).

Common situations: Developing against MySQL 8 and deploying to an older production server; CI images older than production; mixed fleets where some hosts run 5.7; tools assuming uniform introspection support.

Related errors


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

Appendix: source

Thrown at framework/db/mysql/Schema.php:216

    /**
     * {@inheritdoc}
     */
    protected function loadTableUniques($tableName)
    {
        return $this->loadTableConstraints($tableName, 'uniques');
    }

    /**
     * {@inheritdoc}
     */
    protected function loadTableChecks($tableName)
    {
        $version = $this->db->getServerVersion();

        // check version MySQL >= 8.0.16
        if (\stripos($version, 'MariaDb') === false && \version_compare($version, '8.0.16', '<')) {
            throw new NotSupportedException('MySQL < 8.0.16 does not support check constraints.');
        }

        $checks = [];

        $sql = <<<SQL
        SELECT cc.CONSTRAINT_NAME as constraint_name, cc.CHECK_CLAUSE as check_clause
        FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
        JOIN INFORMATION_SCHEMA.CHECK_CONSTRAINTS cc
        ON tc.CONSTRAINT_NAME = cc.CONSTRAINT_NAME
        WHERE tc.TABLE_NAME = :tableName AND tc.CONSTRAINT_TYPE = 'CHECK';
        SQL;

        $resolvedName = $this->resolveTableName($tableName);
        $tableRows = $this->db->createCommand($sql, [':tableName' => $resolvedName->name])->queryAll();

        if ($tableRows === []) {
            return $checks;
        }

View on GitHub (pinned to 66f00d18a2)