{"record":{"id":"3b24d4ceb0aa2cee","repo":"yiisoft/yii2","slug":"mysql-does-not-support-default-value-constraints","errorCode":null,"errorMessage":"MySQL does not support default value constraints.","messagePattern":"MySQL does not support default value constraints\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"framework/db/mysql/Schema.php","lineNumber":257,"sourceCode":"            $check = new CheckConstraint(\n                [\n                    'name' => $tableRow['constraint_name'],\n                    'expression' => $tableRow['check_clause'],\n                ]\n            );\n            $checks[] = $check;\n        }\n\n        return $checks;\n    }\n\n    /**\n     * {@inheritdoc}\n     * @throws NotSupportedException if this method is called.\n     */\n    protected function loadTableDefaultValues($tableName)\n    {\n        throw new NotSupportedException('MySQL does not support default value constraints.');\n    }\n\n    /**\n     * Creates a query builder for the MySQL database.\n     * @return QueryBuilder query builder instance\n     */\n    public function createQueryBuilder()\n    {\n        return Yii::createObject(QueryBuilder::className(), [$this->db]);\n    }\n\n    /**\n     * Resolves the table name and schema name (if any).\n     * @param TableSchema $table the table metadata object\n     * @param string $name the table name\n     */\n    protected function resolveTableNames($table, $name)\n    {","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/mysql/Schema.php#L239-L275","documentation":"The Schema constraint API defines loadTableDefaultValues() for drivers with named DEFAULT constraints; MySQL stores defaults inline in column DDL, so there is nothing to introspect and the method unconditionally throws NotSupportedException. It exists only to satisfy the base-class contract.","triggerScenarios":"Calling $db->getTableDefaultValues('tbl') on a MySQL connection, usually from generic code that walks every constraint accessor (getTablePrimaryKey, getTableForeignKeys, getTableChecks, getTableDefaultValues) against an arbitrary connection.","commonSituations":"Cross-database tooling written against pgsql/mssql (where default-value constraints exist) reused on MySQL; ORM/admin-panel generators probing all constraint kinds.","solutions":["Do not call getTableDefaultValues() on MySQL - the information does not exist.","Read column defaults from the table schema instead: getTableSchema($t)->columns[$name]->defaultValue.","In generic constraint loops, wrap the call in try/catch (NotSupportedException) or branch on driver name."],"exampleFix":"// before\n$defaults = $db->getTableDefaultValues('post');\n\n// after\n$defaults = array_map(\n    fn($c) => $c->defaultValue,\n    $db->getTableSchema('post', true)->columns\n);","handlingStrategy":"try-catch","validationCode":"if ($db->driverName === 'mysql') {\n    // default-value constraints do not exist; read column defaults instead\n    $defaults = array_map(fn($c) => $c->defaultValue, $db->getTableSchema($t, true)->columns);\n} else {\n    $defaults = $db->getTableDefaultValues($t);\n}","typeGuard":null,"tryCatchPattern":"try {\n    $defaults = $db->getTableDefaultValues($table);\n} catch (\\yii\\db\\NotSupportedException $e) {\n    $defaults = []; // driver has no named DEFAULT constraints\n}","preventionTips":["In generic constraint walkers, treat NotSupportedException as 'feature absent', not as a failure.","Prefer column-schema defaults for MySQL; they are always available.","Encode driver capability matrices in schema tooling instead of probing with live calls."],"tags":["yii2","php","mysql","database","default-constraint","not-supported"],"backgroundTag":"unsupported-driver-operation","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}