yiisoft/yii2 · error · NotSupportedException

MySQL does not support default value constraints.

Error message

MySQL does not support default value constraints.

What it means

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.

Source

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

            $check = new CheckConstraint(
                [
                    'name' => $tableRow['constraint_name'],
                    'expression' => $tableRow['check_clause'],
                ]
            );
            $checks[] = $check;
        }

        return $checks;
    }

    /**
     * {@inheritdoc}
     * @throws NotSupportedException if this method is called.
     */
    protected function loadTableDefaultValues($tableName)
    {
        throw new NotSupportedException('MySQL does not support default value constraints.');
    }

    /**
     * Creates a query builder for the MySQL database.
     * @return QueryBuilder query builder instance
     */
    public function createQueryBuilder()
    {
        return Yii::createObject(QueryBuilder::className(), [$this->db]);
    }

    /**
     * Resolves the table name and schema name (if any).
     * @param TableSchema $table the table metadata object
     * @param string $name the table name
     */
    protected function resolveTableNames($table, $name)
    {

View on GitHub (pinned to 66f00d18a2)

Solutions

  1. Do not call getTableDefaultValues() on MySQL - the information does not exist.
  2. Read column defaults from the table schema instead: getTableSchema($t)->columns[$name]->defaultValue.
  3. In generic constraint loops, wrap the call in try/catch (NotSupportedException) or branch on driver name.

Example fix

// before
$defaults = $db->getTableDefaultValues('post');

// after
$defaults = array_map(
    fn($c) => $c->defaultValue,
    $db->getTableSchema('post', true)->columns
);
Defensive patterns

Strategy: try-catch

Validate before calling

if ($db->driverName === 'mysql') {
    // default-value constraints do not exist; read column defaults instead
    $defaults = array_map(fn($c) => $c->defaultValue, $db->getTableSchema($t, true)->columns);
} else {
    $defaults = $db->getTableDefaultValues($t);
}

Try / catch

try {
    $defaults = $db->getTableDefaultValues($table);
} catch (\yii\db\NotSupportedException $e) {
    $defaults = []; // driver has no named DEFAULT constraints
}

Prevention

When it happens

Trigger: 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.

Common situations: Cross-database tooling written against pgsql/mssql (where default-value constraints exist) reused on MySQL; ORM/admin-panel generators probing all constraint kinds.

Related errors


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