{"record":{"id":"13b6244d23f80c16","repo":"yiisoft/yii2","slug":"mysql-8-0-16-does-not-support-check-constraints","errorCode":null,"errorMessage":"MySQL < 8.0.16 does not support check constraints.","messagePattern":"MySQL < 8\\.0\\.16 does not support check constraints\\.","errorType":"exception","errorClass":"yii\\base\\NotSupportedException","httpStatus":null,"severity":"error","filePath":"framework/db/mysql/Schema.php","lineNumber":216,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     */\n    protected function loadTableUniques($tableName)\n    {\n        return $this->loadTableConstraints($tableName, 'uniques');\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    protected function loadTableChecks($tableName)\n    {\n        $version = $this->db->getServerVersion();\n\n        // check version MySQL >= 8.0.16\n        if (\\stripos($version, 'MariaDb') === false && \\version_compare($version, '8.0.16', '<')) {\n            throw new NotSupportedException('MySQL < 8.0.16 does not support check constraints.');\n        }\n\n        $checks = [];\n\n        $sql = <<<SQL\n        SELECT cc.CONSTRAINT_NAME as constraint_name, cc.CHECK_CLAUSE as check_clause\n        FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc\n        JOIN INFORMATION_SCHEMA.CHECK_CONSTRAINTS cc\n        ON tc.CONSTRAINT_NAME = cc.CONSTRAINT_NAME\n        WHERE tc.TABLE_NAME = :tableName AND tc.CONSTRAINT_TYPE = 'CHECK';\n        SQL;\n\n        $resolvedName = $this->resolveTableName($tableName);\n        $tableRows = $this->db->createCommand($sql, [':tableName' => $resolvedName->name])->queryAll();\n\n        if ($tableRows === []) {\n            return $checks;\n        }","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mysql/Schema.php#L198-L234","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Upgrade the MySQL server to 8.0.16 or newer.","Gate the call: skip getTableChecks() when $db->getServerVersion() is below 8.0.16 and the driver is mysql.","Catch NotSupportedException in cross-version introspection code and treat checks as an empty list.","On MariaDB no action is needed - the version guard already excludes it."],"exampleFix":"// before\n$checks = $db->getTableChecks('order');\n\n// after\n$version = $db->getServerVersion();\n$isMySql = stripos($version, 'MariaDb') === false;\n$checks = ($isMySql && version_compare($version, '8.0.16', '<'))\n    ? []\n    : $db->getTableChecks('order');","handlingStrategy":"validation","validationCode":"function mysqlSupportsCheckIntrospection(\\yii\\db\\Connection $db): bool\n{\n    $version = $db->getServerVersion();\n    return stripos($version, 'MariaDb') !== false\n        || version_compare($version, '8.0.16', '>=');\n}\n\n$checks = mysqlSupportsCheckIntrospection($db) ? $db->getTableChecks('order') : [];","typeGuard":null,"tryCatchPattern":"try {\n    $checks = $db->getTableChecks('order');\n} catch (\\yii\\db\\NotSupportedException $e) {\n    $checks = []; // MySQL < 8.0.16: CHECK constraints are parsed but not stored\n}","preventionTips":["Pin the minimum MySQL version in deployment docs and CI docker images to 8.0.16+.","Feature-detect server capabilities instead of assuming uniform introspection support.","Include the server version in bug reports for schema-introspection failures."],"tags":["yii2","php","mysql","database","check-constraint","version-mismatch"],"backgroundTag":"unsupported-database-version","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}