yiisoft/yii2 · error · NotSupportedException

PostgreSQL does not support default value constraints.

Error message

PostgreSQL does not support default value constraints.

What it means

yii\db\pgsql\Schema::loadTableDefaultValues() implements the base Schema's named-DEFAULT-constraint contract, but PostgreSQL, like MySQL, expresses defaults in column DDL rather than as separate constraint objects. The method therefore unconditionally throws NotSupportedException.

Solutions

  1. Do not call getTableDefaultValues() on PostgreSQL.
  2. Read per-column defaults from getTableSchema($t)->columns[$name]->defaultValue.
  3. Branch on driver name or catch NotSupportedException in generic introspection loops.

Example fix

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

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

Strategy: try-catch

Validate before calling

if ($db->driverName === 'pgsql') {
    $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 = []; // pgsql has no named default constraints
}

Prevention

When it happens

Trigger: Calling $db->getTableDefaultValues('tbl') on a PostgreSQL connection, usually from generic code that enumerates all constraint accessors without driver awareness.

Common situations: Schema tooling or admin generators written against mssql (the driver that does support named defaults) reused on PostgreSQL; migration diff engines probing every constraint type.

Related errors


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

Appendix: source

Thrown at framework/db/pgsql/Schema.php:287

    {
        return $this->loadTableConstraints($tableName, 'uniques');
    }

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

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

    /**
     * Creates a query builder for the PostgreSQL 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)